@Html. Partial, @Html. Action, @Html. renderpartial, @Html. Renderaction differences.

Source: Internet
Author: User

MVC renderaction RenderPartial

Both the html.renderpartial and Html.renderaction methods are used to embed user controls on the interface.

Html.renderpartial is to embed the user control directly on the interface:

<%html.renderpartial ("Logonusercontrol");%>

Or

<%html.renderpartial ("~/areas/comm/views/shared/logonusercontrol.ascx");%>

Note: When using the first method, the user control must be placed in the same directory as the caller, or it can be placed in the view/shared.

Html.renderaction invokes the user control through the action in the controller

Controller:----The controller where the user control is located

Public ActionResult UserControl ()
{
return Partialview ();
}

View:----Call the user control's view

<%html.renderaction ("UserControl", "Controller");%>

Personally prefer to use renderpartial

RenderPartial and Renderaction are all methods used to display Partialview in ASP. NET MVC, so when to use which method is the first problem encountered. And to make the right choice, we need to have a good understanding of both, and know its similarities and differences. This is also the subject of this article. What is html.renderpartial

Html.renderpartial is used in ASP. NET MVC to call Partialview. Partialview is basically the UserControl in ASP. WebForm. The invocation is also simple, as long as the Partialview name is passed as a parameter in the view. Like what:

<% Html.RenderPartial("YourPartialView", YourData); %>

The yourdata is an optional parameter. If so, Yourdata will be assigned to the model in Partialview. If not, then mode and ViewData in the view that calls RenderPartial are passed to Partialview. That is, the Partialview data comes from the called View.

What is Html.renderaction

Html.renderaction allows you to invoke an action directly and display the returned result directly in the currently called View. Like what:

<% Html.RenderAction("Show", "Tag"); %>

At this point, the Show method in Tagcontroller is called. Because this is called an action method, you can do all the things you want to do in this method, such as getting data from a database, a file, writing data, and returning results.

[OutputCache(Duration=6000)]publicActionResult Show(){    var tagData = null;    //Get data from database    //tagData = tagService.AllHot();    returnPartialView("TagCloud", tagData);}

Tagcloud is a simple Partialview file.

The same point of the two

RenderPartial and renderaction are often used to display a relatively independent "block", such as a menu or a navigation bar. The results of both outputs are displayed as part of the view that is called.

Different points of the two
    1. Renderpatial's data comes from the calling view, and renderpatial comes from itself.
    2. Renderaction will initiate a new request, and renderpatial will not.
How to choose

Based on the 2nd of the differences, because Renderaction invokes a new action method, the action in ASP. NET MVC is the smallest cache unit, so if the data for a "block" is fixed, it doesn't change depending on the visitor. So this is the time to use Renderaction. aside, for Renderaction to launch a new request, it feels a bit destructive to the process of invoking the page. When a view is displayed, it launches a request to get the data to display, apparently undermining the principle of being a view:

A View should only know how to render, and not what to render!
In ASP. @html.partial, @Html. Action, @Html. renderpartial, @Html. Renderaction the difference between the four to make a summary, clean up the idea, convenient for later use:

1. Method with render @Html. Partial corresponds to @{html.renderpartial (...);}
@Html. Action corresponds to @{html.renderaction (...);}

2, action, renderaction Load Method view, execute 3. Html.partial can provide the user control name directly as a parameter, and html.action needs a corresponding action to return Partailresult (that is, Retun Partialview ()) within the action.

4, for a simple user control without any logic, it is recommended to use html.partial; for user controls that need to set some model, Html.action is recommended. Of course, there are model data can also use the Html.partial method, you can see the overloads of the method.

5, the use of html.action has the advantage that can be based on different scenarios to choose a different user control. Like what:
@Html. Action ("Userinfocontrol")
In the corresponding Userinfocontrol this action, when the user is not logged in, you can Retun Partialview ("Logonusercontrol"), after logging in, you can Retun Partialview (" Userinfocontrol ");

@Html. Partial, @Html. Action, @Html. renderpartial, @Html. Renderaction. (EXT)

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.