Add a view
In this section, you will continue to modify the HelloWorldController class and use the view template to encapsulate
Simple HTML response to the client.
You will useRazor view EngineCreate a view template.
Based onRazor view EngineTemplate. CshtmlExtension, which provides a very elegant
Use C # To generate HTML output. When you write a view template,RazorHit you
The number of keyboards is minimized,Coding is fast and smooth.
We use the view template from the Index method in the HelloWorldController class of the controller.
Now the Index method returns only one hardcoded string message. Change the Index method,
Let it return a View object. The Code is as follows:
public ActionResult Index()
{
return View();
}
These codes use view templates to generate HTML responses to the client browser. In this project,
You can add a view template to the Index method. Right-click the Index method and select
Add a view.
The add view dialog box is displayed. Retain the default options and click Add.
In the folderMvcMovieViewsHelloWorldFileMvcMovieViewsHelloWorldIndex. cshtml
Created. You can see in Solution Explorer.
The following showsIndex. cshtmlFile Created:
In the tag<H2>
Add some HTML.MvcMovieViewsHelloWorldIndex. cshtmlThe file is as follows:
As shown in:
@{
ViewBag.Title = "Index";
}
<p>Hello from our View Template!</p>
Run the program and browse the HelloWorld controller (Http: // localhost: xxxx/HelloWorld).
The Index method does not work much. It only runs an expression.Return View (),Expression
Indicates that this method will use the view template to send a response to the browser.
Because you do not specify which view template to use, ASP. net mvc uses it by default.ViewsHelloWorld
FolderIndex. cshtmlView. Displays hard encoding in the view.
This looks great! However, the title bar of the browser displays Index and the title of the large size.
"My MVC Application.", let's change them!
Next section: Asp.net MVC3.0 Getting Started Guide 3.2 View