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. Return a view object by changing the index method, as shown in the following example.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 Controller method (also called Action Method). The above index method generally returns an actionresult (orActionresultThe inherited type), rather than the original type, such as 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 <H2> tag.

<P> hello from our view template! </P>

CompleteMvcmovie \ helloworld \ index. cshtmlFile is as follows.

@{
Viewbag. Title = "Index ";

}

<H2> index </H2>

<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".

 

Download the complete document:ASP. NET mvc41_1_timeout

Bytes --------------------------------------------------------------------------------------------------------------------

Note:

9 articles in this seriesArticle, 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. Introduction to ASP. NET mvc4

· Original address: http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/intro-to-aspnet-mvc-4

· Address: http://www.cnblogs.com/powertoolsteam/archive/2012/11/01/2749906.html

2. Add a controller

· Original address: http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-controller

· Address: http://www.cnblogs.com/powertoolsteam/archive/2012/11/02/2751015.html

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

4. Add a model

· Original address: http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-model

· Address: http://www.cnblogs.com/powertoolsteam/archive/2012/12/17/2821495.html

5. Access the data model from the Controller

· Original address: http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/accessing-your-models-data-from-a-controller

· Address: http://www.cnblogs.com/powertoolsteam/archive/2013/01/11/2855935.html

6. Verify the editing method and view

· Original address: http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/examining-the-edit-methods-and-edit-view

· Address: http://www.cnblogs.com/powertoolsteam/archive/2013/01/24/2874622.html

7. Add new fields to the movie table and Model

· Original address: http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-new-field-to-the-movie-model-and-table

· Translation address:Http://www.cnblogs.com/powertoolsteam/archive/2013/02/26/2933105.html

8. Add a validator to the Data Model

· Original address: http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-validation-to-the-model

· Address: http://www.cnblogs.com/powertoolsteam/archive/2013/03/05/2944030.html

9. query details and delete records

· Original address: http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/examining-the-details-and-delete-methods

· Address: http://www.cnblogs.com/powertoolsteam/archive/2013/03/07/2948000.html

10. Third-party control studio for ASP. NET wijmo mvc4 tool Application

 

Address: http://www.cnblogs.com/powertoolsteam/archive/2013/05/09/3068699.html

 

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.