Asp. Net MVC4 Getting Started Guide (3): Add a view

Source: Internet
Author: User

In this section, you need to modify the HelloWorldController class to use the view template file to generate the HTML returned to the client browser through a clean and elegant encapsulation.

You will create a view template file using the Razor view engine introduced by ASP. net mvc 3. The Razor view template file uses the. cshtml file extension and provides an elegant way to use the C # language to create the HTML to be output. When you use Razor to write a view template file, you can minimize the number of characters and keyboard percussion required, and implement a fast and smooth coding workflow.

The Index method in the Controller class returns a hard-coded string. Change the Index method to return a View object, as shown in the following sample code:

?

The Index method above uses a view template to generate an HTML and return it to the browser. The Controller method is also called action method. The above Index method generally returns an ActionResult or the type inherited from the ActionResult), rather than the original type, such as a string.

In this project, you can use the Index method to add a view template. To do this, right-click the Index method and click"Add View".

650) this. width = 650; "style =" border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-right -: "title =" clip_image001 "border =" 0 "alt =" clip_image001 "width =" 474 "height =" 234 "src =" http://www.bkjia.com/uploads/allimg/131228/122949B17-0.png "/>

AppearsAdd ViewDialog box. Retain the default value, and clickAddButton:

650) this. width = 650; "style =" border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-right -: "title =" clip_image002 "border =" 0 "alt =" clip_image002 "width =" 547 "height =" 547 "src =" http://www.bkjia.com/uploads/allimg/131228/1229493G7-1.png "/>

You canSolution Resource ManagerThe MvcMovie \ HelloWorld folder and the created MvcMovie \ View \ HelloWorld \ Index. cshtml file are displayed:

650) this. width = 650; "style =" border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-right -: "title =" clip_image003 "border =" 0 "alt =" clip_image003 "width =" 222 "height =" 464 "src =" http://www.bkjia.com/uploads/allimg/131228/12294a4F-2.png "/>

The created Index. cshtml file is displayed:

650) this. width = 650; "style =" border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-right -: "title =" clip_image004 "border =" 0 "alt =" clip_image004 "width =" 685 "height =" 623 "src =" http://www.bkjia.com/uploads/allimg/131228/1229493451-3.png "/>

Add the following HTML after the


Hello from our View Template!


CompleteMvcMovie \ HelloWorld \ Index. cshtmlFile is as follows.

@{
ViewBag. Title = "Index ";
}
<H2> Index

Hello from our View Template!


Note: If you are using Internet Explorer 9, you will not see the highlighted yellow

Hello from our View Template!

, Click "Compatibility View" button 650) this. width = 650; "style =" border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-right -: "title =" clip_image005 "border =" 0 "alt =" clip_image005 "width =" 20 "height =" 20 "src =" http://www.bkjia.com/uploads/allimg/131228/1229494J8-4.jpg "/>, in IE browser, icon from 650) this. width = 650; "style =" border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-right -: "title =" c Lip_image005 [1] "border =" 0 "alt =" clip_image005 [1] "width =" 20 "height =" 20 "src =" http://www.bkjia.com/uploads/allimg/131228/1229493004-5.jpg "/> changes to 650 of the solid color) this. width = 650; "style =" border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-right -: "title =" clip_image006 "border =" 0 "alt =" clip_image006 "width =" 20 "height =" 20 "src =" http://www.bkjia.com/uploads/allimg/131228/1229492923-6.jpg" /> Icon. You can also view this tutorial in Firefox or Chrome.


If you are using Visual Studio 2012, right-click Solution ExplorerIndex. cshtmlFile, and select"View in Page Checker".


--------------------------------------------------------

Note:

This series contains nine articles translated from Asp. net MVC4 official tutorial, due to the concise description of this series of articles, the length is moderate, from an example to explain, the full text finally completed a small system for managing movies, very suitable for beginners Asp.. Net MVC4. Nine articles:

1. asp. net MVC4 entry introduction-original address: http://www.cnblogs.com/powertoolsteam/archive/2012/11/01/2749906.html2. add a controller. Source Address: http://powertoolsteam.blog.51cto.com/2369428/10480563. add a view-original address: http://powertoolsteam.blog.51cto.com/2369428/10513794. add a model-original address: http://powertoolsteam.blog.51cto.com/2369428/10919075. access the data model from the Controller-Source Address: http://powertoolsteam.blog.51cto.com/2369428/11144016. verify the editing method and edit view-original address: http://powertoolsteam.blog.51cto.com/2369428/11253907. add a new field to the movie table and model. add a validator to the data model-Source Address: http://powertoolsteam.blog.51cto.com/2369428/11474499. query details and delete records-original address: http://powertoolsteam.blog.51cto.com/2369428/1149311

10. Third-party control Studio for ASP. NET Wijmo MVC4 tool Application

Address: http://powertoolsteam.blog.51cto.com/2369428/1196469



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.