[Official tutorial] Asp. Net MVC4 Getting Started Guide (3): Add a view and mvc4 Getting Started Guide
3. Add a view
· Original address: http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-view
· Address: http://www.cnblogs.com/powertoolsteam/archive/2012/11/06/2756711.html
Asp. Net MVC4 Getting Started Guide (3): Add a view
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:
public ActionResult Index() { return View(); } |
The Index method above uses a view template to generate an HTML and return it to the browser. The method of the Controller (also called action method). The above Index method generally returns an ActionResult (or the type inherited from the ActionResult) instead of 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".
AppearsAdd ViewDialog box. Retain the default value, and clickAddButton:
You canSolution Resource ManagerThe MvcMovie \ HelloWorld folder and the created MvcMovie \ View \ HelloWorld \ Index. cshtml file are displayed:
The created Index. cshtml file is displayed:
Add the following HTML after the
<P> Hello from our View Template! </P>
CompleteMvcMovie \ HelloWorld \ Index. cshtmlFile is as follows.
@{
ViewBag. Title = "Index ";
}
<H2> Index <P> Hello from our View Template! </P>
Note: If you are using Internet Explorer 9, you will not see the <p> Hello from our View Template highlighted in yellow! </P> click "Compatibility View". In ie, the icon changes from solid color to solid color. 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".
View is a way to display data. Only by mastering this knowledge can you better use it for MVC development. At the same time, you can also use some development tools to help the development process. ComponentOne Studio ASP. NET MVC This lightweight control can be seamlessly integrated with Visual Studio and fully compatible with MVC6 and ASP. NET 5.0, greatly improving development efficiency.