Introduction to view generation in asp.net mvc

Source: Internet
Author: User

In ASP.net MVC, we divided the front end rendering into three separate parts, Controller used to control the user's actions, and View is used to control what is rendered, and Model is used to represent the data being processed.

From controller to view

Typically, in Controller, we define multiple action, and the return type of each action is generally actionresult, and at the end of the action process, we return the call to the view.

Public ActionResult Index ()

{

return this. View ();

}

By default, a view with the same name as the action will be invoked, for example, in the action above, the view named Index will be used.

If we pass a string argument, then the string is returned as the name of the view, for example, we want to render using a view named Indexview, which can be done as follows.

Public ActionResult Index ()

{

return this. View ("Indexview");

}

Layout and view

Will MVC go straight to our view? No!

After returning from the Action, first, MVC will see if there is a special file named _viewstart.cshtml in the Views folder, and if so, execute the file back.

By default, the contents of this file are as follows:

@{

Layout = "~/views/shared/_layout.cshtml";

}

In other words, it sets up our default layout using that file file.

The layout is equivalent to the master page that we use in WebForm.

If this file is not available, the layout is not used by default.

What if we have this file and we don't want to use the layout on the page? Very simply, set Layout = NULL in the page to overwrite it.

@{

Layout = null;

}

partitions in a layout

In the layout page, there is a special instruction @RenderBody (), which means that the content that you present in the content page will be output here. This is when you use the layout, you will find that there is no reason for

<body>

@RenderBody ()

</body>

If we want to output in more than one position in the layout, for example, the style sheet is usually in the

The solution is to partition, in fact, like the ContentPlaceHolder in WebForm, to define a named area.

For example, here we define a named range called scripts, and the second argument indicates that the area is an optional area, and there is no need to provide content in the content page.

<body>

@RenderBody ()

@RenderSection ("Scripts", Required:false)

</body>

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/aspx/

In our content view, all of the content in the default is the Renderbody part that fills the layout page.

If not, you can define what you want to output to the corresponding area in the content page by @section the region name {}.

@section scripts{

<script type= "Text/javascript" >

var i = 0;

</script>

}

Partial view

If our pages are more complex, defining the output in the same view can cause the page to be very complex, and by dividing the page into more than one separate component, a complex page may be split into multiple sub sections to render. such as the title section of the page, the menu of the page, the footer section of the page, and so on. In WebForm, the technology to solve this problem is called the user control, which is solved by two techniques in MVC: Partial view and child Action.

To see the partial view first, the partial view is not dependent on the Action, it can only be used in other standalone views, you can create a partial view directly in the View folder.

In a page that uses partial view, call the partial view using the RenderPartial method.

Html.renderpartial ("Productsummary");

The partial view shares all of the data, such as models, Viewdata,viewbag, and so on, with the main view, which can be used directly in the partial views.

If you want the partial view to use different model objects to simplify the data complexity in the partial view, you can also pass a model object directly past.

Html.renderpartial ("Productsummary", p);

The second argument here will be used in the partial view when it is in the model object.

It is important to note that the return type of the RenderPartial method is void, which outputs the output directly into the output stream of the response. So when using the above statement, you can't embed it directly in the page, you need to put it in an @ {} statement, as shown below.

@{

Html.renderpartial ("Productsummary", p);

}

If you don't like the syntax, and you want to embed it directly into the page, you can also use the Html.partial method to render, except that this method returns the rendered Html fragment rather than directly to the output stream.

@Html. Partial ("Productsummary", p)

Child Action Problem

Because the distributed view has no processing power, can only inherit the data from the main page to render, if you need to have processing power, how to do? In WebForm, we can use Server.Execute to embed the output of other handlers, and in MVC we use the child Action to handle it.

A child action is an action method that a user cannot access directly and can only be accessed in another action, as with a normal action, with an action method and a view of the action.

We use feature childactiononly to show that this is a child Action.

[Childactiononly]

public string Menu ()

{

Return "Hello, from Navcontroller";

}

If the user requests this child Action directly, the system returns an error message.

Operation ' Menu ' can only be accessed by child request.

In other views, you can use Renderaction to access the child Action, similar to the RenderPartial method, which outputs the content directly into the output stream, and needs to be used @{} because their return type is void.

@{html.renderaction ("menu", "Nav");}

Similarly, using an Action can return an Html fragment directly, without using a @{}.

<!--Action-->

@Html. Action ("menu", "Nav")

Author: cnblogs Champion

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.