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");%>
renderpartialAnd
renderactionare used in ASP. NET MVC to display
Partialviewmethod, 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
- Renderpatial's data comes from the calling view, and Renderaction comes from itself.
- 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!
It's over.
How to choose Html.renderpartial and Html.renderaction