Asp.net MVC3.0 Getting Started Guide 3.1 View

Source: Internet
Author: User

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

Related Article

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.