How to Select Html. RenderPartial and Html. RenderAction, html. renderpartial
Both Html. RenderPartial and Html. RenderAction are used to embed user controls on the interface.
Html. RenderPartial is to directly embed the user control into 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 of the caller, or in View/Shared.
The Html. RenderAction calls the user control through the Action in the Controller.
Controller: ---- Controller of the User Control
Public ActionResult UserControl () {return PartialView ();}
View: ---- call the View of the user control
<% Html. RenderAction ("UserControl", "Controller"); %>
RenderPartialAnd
RenderActionThey are all used in Asp.net Mvc to display
PartialViewSo when to use which method is the first problem. To make the right choice, you must have a full understanding of both of them and understand their similarities and differences. This is also the topic of this article. What is Html. RenderPartial
Html. RenderPartial is used to call PartialView in Asp.net Mvc. PartialView is basically the UserControl in Asp.net Webform. Calling is also very simple. You only need to pass the PartialView name as a parameter in the View. For example:
<% Html.RenderPartial("YourPartialView", YourData); %> |
YourData is an optional parameter. If yes, YourData will be assigned to the Model in PartialView. If no, the Mode and ViewData in the View that calls RenderPartial will be passed to PartialView. That is to say, the data of PartialView comes from the called View.
What is Html. RenderAction
Html. RenderAction allows you to directly call an Action and display the returned result in the View of the current call. For example:
<% Html.RenderAction("Show", "Tag"); %> |
The Show method in TagController is called. Because an Action method is called at this time, you can complete the operations you want to complete in this method, such as getting data from databases, files, writing data, and returning results.
[OutputCache(Duration=6000)]public ActionResult Show(){ var tagData = null; //Get data from database //tagData = tagService.AllHot(); return PartialView("TagCloud", tagData);} |
TagCloud is a simple PartialView file.
Similarities
RenderPartial and RenderAction are usually used to display a relatively independent "Block" of a function, such as display menu or navigation bar. Both outputs are displayed as part of the called View.
What are the differences between the two?
According to the second point in the two differences, because RenderAction will call a new Action method, and Action in Asp.net Mvc is the smallest cache unit, if the data of a "Block" is relatively fixed, it will not change because of different visitors, so it is time to use RenderAction. In other words, RenderAction will initiate a new Request, which may damage the page calling process. When a View is displayed, another Request is initiated to obtain the data for display, which obviously breaks the principle of being a View:
A View should only know how to render, but not what to render!