ASP. MVC4 Getting Started Guide (3): Adding a View

Source: Internet
Author: User

In this section, you need to modify the Helloworldcontroller class to use the view template file, which generates a clean and elegant wrapper to return the HTML to the client browser.

You will create a view template file that uses 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 create the HTML that you want to output using the C # language. When writing a view template file with razor, the required number of characters and keystrokes is minimized and a fast and smooth coding workflow is achieved.

The index method currently in the Controller class returns a hard-coded string. Changing the index method returns a View object, such as the following sample code:

 Public ActionResult Index () {     return  View ();}

The index method above uses a view template to generate an HTML return to the browser. The controller's method (also known as action method), such as the index method above, typically returns a ActionResult (or type inherited from 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 in the index method, and then click Add View.

The Add View dialog box appears. Leave the default values and click the Add button:

You can see the Mvcmovie\helloworld folder and the mvcmovie\view\helloworld\index.cshtml file that has been created in Solution Explorer:

The index.cshtml file that has been created is displayed:

Add the following HTML after the

< P > Hello from our View template! </ P >

The complete mvcmovie\helloworld\index.cshtml file is shown below.

@{     <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!</p> highlighted in yellow above, and click the "Compatibility View" button in IE browser, The icon changes from the icon to a solid color. Alternatively, you can view this tutorial in Firefox or Chrome.

If you are using Visual Studio 2012, in Solution Explorer, right-click the index.cshtml file and select View in Page Inspector.

Full document Download: ASP. MVC4 Getting Started Guide. pdf

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

Translator Note:

This series of 9 articles, translated from the official ASP. NET MVC4 tutorial, because this series of articles concise, space moderate, from an example to explain, the full text finally completed a small system to manage the film, very suitable for the novice MVC4 ASP, and start the development work. 9 Articles for:

1. Introduction to ASP. 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. Accessing 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. Validating editing methods and editing views

· 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 a new field 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

· Address: http://www.cnblogs.com/powertoolsteam/archive/2013/02/26/2933105.html

8. Adding 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. Wijmo MVC4 Tools App

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

ASP MVC4 Getting Started Guide (3): Adding a 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.