View generation instance analysis and MVC instance analysis in ASP. NET mvc

Source: Internet
Author: User

View generation instance analysis and MVC instance analysis in ASP. NET mvc

This document analyzes the view generation process in ASP. net mvc. Share it with you for your reference. The details are as follows:

In ASP. net mvc, we divide the front-end presentation into three independent parts for implementation. The Controller is used to control user operations, the View is used to control the presentation content, and the Model is used to represent the processed data.

From controller to view

Generally, in Controller, we define multiple actions. The return type of each Action is generally ActionResult. At the end of Action processing, we return a call to the view.
Copy codeThe Code is as follows: public ActionResult Index ()
{
Return this. View ();
}
By default, a view with the same name as the Action is called. For example, the view named Index is used in the preceding Action.
 
If we pass a string parameter, the string will be considered as the view name. For example, if we want to use the view named IndexView for rendering, we can do the following.
Copy codeThe Code is as follows: public ActionResult Index ()
{
Return this. View ("IndexView ");
}
Layout and view

Will MVC go directly to our view? No!
 
After the Action is returned, MVC first checks whether a special file named _ ViewStart. cshtml exists in the Views folder. If yes, It executes the file again.
 
By default, the content of this file is as follows:
Copy codeThe Code is as follows :@{
Layout = "~ /Views/Shared/_ Layout. cshtml ";
}
That is to say, it sets our default layout to use that file.
 
The layout is equivalent to the master page we use in WebForm.
 
If this file is not available, no layout is used by default.
 
What if we don't want to use this layout on the page with this file? It's easy to set Layout = null on the page to overwrite it.
Copy codeThe Code is as follows :@{
Layout = null;
}
Partitions in the layout

On the layout page, a special command @ RenderBody () is returned by default. This means that the content you present on the content page will be output here. This is why Copy codeThe Code is as follows: <body>
@ RenderBody ()
</Body>
For example, a style sheet is usually in the  
The solution is partitioning. In fact, like ContentPlaceHolder in WebForm, it defines a named region.
 
For example, the following defines a named region named scripts. The second parameter indicates that this region is an optional region and does not need to be provided on the Content Page.
Copy codeThe Code is as follows: <body>
@ RenderBody ()
 
@ RenderSection ("scripts", required: false)
</Body>
In our content view, by default, all content is filled into the RenderBody section on the layout page.
 
If not, you can define the content to be output to the corresponding area by @ section region name {} on the Content Page.
Copy codeThe Code is as follows: @ section scripts {
<Script type = "text/javascript">
Var I = 0;
</Script>
}
Division View

If the page is complex, defining the output content in the same view will make the page very complex. By dividing the page into multiple independent components, you can split a complex page into multiple child parts for presentation. For example, the page title, page menu, and page footer. In WebForm, the technology used to solve this problem is called the user control. In MVC, two technologies are used to solve this problem: the Division view and sub-Action.
 
First, let's look at the segment view. The segment view is not dependent on Action and can only be embedded in other independent views. You can directly create the segment view in the view folder.
 
On the page that uses the partial view, use the RenderPartial method to call the partial view.
Copy codeThe Code is as follows: Html. RenderPartial ("ProductSummary ");
The branch view shares all the data with the main view, such as the model, ViewData, and ViewBag. The data can be directly used in the branch view.
 
If you want the branch view to use different model objects to simplify the data complexity in the branch view, you can also directly pass a model object.
Copy codeThe Code is as follows: Html. RenderPartial ("ProductSummary", p );
The second parameter will be used in the Division view as a model object.
 
Note that the response type of the RenderPartial method is void, which directly outputs the output content to the response output stream. Therefore, when using the preceding statement, it cannot be directly embedded into the page. You need to put it in a @ {} statement, as shown below.
Copy codeThe Code is as follows :@{
Html. RenderPartial ("ProductSummary", p );
}
If you do not like this syntax and want to embed it directly into the page, you can also use Html. the difference between the Partial method and the Partial method is that this method returns the rendered Html segment instead of directly output to the output stream.
Copy codeThe Code is as follows: @ Html. Partial ("ProductSummary", p)

Sub-Action problems

Because the distribution view does not have the processing capability, it can only inherit the data on the home page for presentation. What should we do if we need the processing capability? In WebForm, we can use Server. Execute to embed the output content of other processing programs. In MVC, we use sub-Actions for processing.
 
A sub-Action is an Action method that can only be accessed in other actions. It is the same as a common Action, with an Action method and a view corresponding to this Action.
 
We use the ChildActionOnly feature to describe that this is a subaction.
Copy codeThe Code is as follows: [ChildActionOnly]
Public string Menu ()
{
Return "Hello, from NavController ";
}
If you directly request this subaction, the system returns an error message.
 
The "menu" operation can only be accessed by a subrequest.

In other views, you can use RenderAction to access sub-actions. Similar to the RenderPartial method, this method directly outputs the content to the output stream, because their return types are void, so you need to use.
Copy codeThe Code is as follows: @ {Html. RenderAction ("Menu", "Nav ");}
Similarly, with Action, you can directly return an Html clip without using.
Copy codeThe Code is as follows: <! -- Action -->
@ Html. Action ("Menu", "Nav ")

I hope this article will help you design your asp.net program.

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.