Chapter 2 of pro ASP. NET mvc4 4th Edition (II)

Source: Internet
Author: User

  This article is available at http://www.cnblogs.com/outtamyhead/archive/2013/03/21/2973205.html. You need to reserve this address for the download.

Above:

1. because it was the first time to translate the entire book, it is inevitable that you may make mistakes. I hope you will give me some suggestions. If the translation is poor, I hope you will not be more encouraged.

2. This series does not follow the literal translation of the original text, but adds some of my words to it (without changing the original intention). Therefore, you may wish to compare them.

3. I have published one or two blogs every week in this series. I have been working very busy recently and have been working overtime.

4. This series does not provide the original text. If you want to view the original text, you can download the PDF on your own.

5. This series saves the previous nonsense and goes straight to the main content.

Chapter 2: The first ASP. NET mvc4 Program (medium)

Rendering web pages

  The preceding column is not output from HTML-it is just a "hello World" string. To generate an HTML response for a browser request, we need to add a view.

  Add and render views

  The first thing we need to do is to modify our index action method, such as List 2-3.

Listing 2-3 using system; using system. Collections. Generic; using system. LINQ; using system. Web; using system. Web. MVC; namespace partyinvites. controllers {public class homecontroller: controller {publicViewresultIndex () {returnView ();}}}

The modified section in Listing 2-3 is highlighted in bold. When we return a viewresult from the action method, we have executed the MVC command to render this view. We created a viewresult without parameters by calling the view method. This tells MVC to render the render page for this action.

If you run the program at this time, you will find that the MVC Framework will try to find a development page to use. Error message from 2 to 8.

  

This error message is very helpful. It not only tells us that MVC cannot find a proper view for our action methods, but also tells us where MVC came to look for this view. This is another good example of MVC Conventions: views are associated with Action methods through naming conventions. Our action method is called index, our controller is called home, And we can know from 2 to 8 that MVC is trying to find the files named index in the [views] folder.

To add a view, stop debugging and run it in homecontroller. in the CS code file, right-click the action method (whether on the method name or in the method body) and select add view. The add view dialog box is displayed, from 2 to 9.

  

Cancel the use a layout or master page selection. We do not use layout in this example. We will talk about this in Chapter 7th. Click Add. Visual Studio will create a new file named index. cshtml in the views-home folder. If you look at the error information from 2 to 8, you will find that the new file is one of the files that MVC is trying to find.

Tip: The. cshtml extension indicates the C # view processed by razor. The previous version of MVC is dependent on the aspx attempt engine, and the View File Extension is. aspx.

Visual Studio automatically opens the index. cshtml file. You will see that most of them are HTML, and the difference looks like this:

@{Layout = null;}

This is an expression that can be parsed by the razor view engine. This is a small example. It only tells razor that we have not selected the master page. We will mention razor later. Add the bold parts in Listing 2-4 to index. cshtml.

@{Layout = null;}<!DOCTYPE html>

The added part is used to display another simple message. Select Start debugging to start the program and test our view. You will see something similar to 2-10.

  

When we edit the index Action Method for the first time, it returns a string. This indicates that MVC does not do anything except passing a string to the browser. Now this index method returns a viewresult. We use MVC to render a view and return HTML. We didn't tell MVC which view we used, so MVC found an automatic match based on the naming convention. This Convention means that the view has an action method name and is located in a folder named after the Controller ---~ /Views/home/index. cshtml.

Besides the string and viewresult, we can return other results from the action method. For example, if we return a redirectresult, We will redirect the browser to another URL. If we return an httpunauthorizedresult, we will force the user to log on.

These classes are collectively referred to as action results, and they are derived from the actionresult class. The action result system allows us to encapsulate and reuse common responses in actions. In this book, we will let you understand them and demonstrate some complex applications.

  

  Add dynamic output

  The goal of a web platform is to build and Display Dynamic Output. In MVC, this job is taken by the Controller, which constructs data and transmits it to the view, while the view is rendered to the HTML.

There is one way to pass data from the Controller to the view, that is, using viewbag, viewbag is a member of the controller base class. Viewbag is a dynamic object. You can specify any attribute to make these values available in the view to be rendered later. In Listing 2-5, some simple dynamic data is transmitted in homecontroller. CS.

List 2-5 using system; using system. collections. generic; using system. LINQ; using system. web; using system. web. MVC; namespace partyinvites. controllers {public class homecontroller: controller {public viewresult index () {int hour = datetime. now. hour; viewbag. greeting = hour <12? "Good morning": "Good afternoon"; return view ();}}}

When we assign a value to the viewbag. Greeting attribute, data is provided to the view. Viewbag is a representative of dynamic objects. The greeting attribute does not exist, until we assign a value-this allows us to pass the value from the Controller to the view in a free and smooth manner before defining the class.

We use the viewbag. Greeting attribute again in the view to obtain the value, for example, list 2-6.

Listing 2-6 @ {layout = NULL;} <! Doctype HTML> 

Added to listing 2-6 is a razor expression. When we call the view method in the Controller's index method, the MVC Framework locates the index. the cshtml view requires the razor view engine to parse the content in the file. Razor will find the expression we added and execute them. In this example, the executed expression indicates the value of the viewbag. Greeting attribute assigned to us.

There is nothing special about the greeting attribute name. You can replace it with any other attribute name and it will take effect, in addition, we can assign multiple attributes to realize the idea of transferring multiple values from the Controller to the view. We can run the project to see our first dynamic MVC output, 2-11.

  

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.