Partial and renderpartial:
The two are of the same nature. They only mean to insert one view into each view, but the return value is a little different.
An object (mvchtmlstring) returned by partial, returns a string, returns a bunch of HTML, and writes it to the home page.
@ Html. Partial ("viewname ")
Renderpartial returns void, which adds the specified view on the home page.
@{
Html. renderpartial ("viewname ");
}
The two methods do not use the controller, but directly add a view (page ).
The renderaction is a bit different. This is an action, so the controller is used to return a page.
Public class childactiononlytestcontroller: Controller
{
[Childactiononly]
Public actionresult getsupplierlist ()
{
VaR controller = new suppliercontroller ();
Return controller. supplierlist ();
}
}
Renderpartial and renderactionSimilarities
It is usually used to display a relatively independent "Block", for example, display menu or navigation bar. Both outputs are displayed as part of the called view.
Differences between the two
- Renderpatial data comes from the called view, while renderaction comes from itself.
- Renderaction initiates a new request, but renderpatial does not.
@ Html. Partial/@ {htmt. renderpartial ()}@ HTML. partial is used to render the partial view as a string @ {HTML. renderpartial} writes the distributed view directly to the response output stream, so it can only be placed in the code block directly, not in the expression (return value is void) renderpartial because it is directly written in the response stream, so the performance is better (slightly affected), and partial does not need to be written in the code block, so it is more convenient
@ Html. Action ()/@ html. renderaction ()It is similar to partial and renderpartial, but because it uses action, it is more flexible and can use the Controller context. In the action, ischildaction can be used to determine whether the request is directly called by URL or called by Action ()/renderaction (). You can use the overload function to directly send parameters to the action. Renderaction takes precedence over actionnameattribute. Use return partialview () in action to specify the partial view. The layout specified in _ viewstatrt. cshtml is invalid. Comparison of four types of partialview:
Partialview:
<div>Just a PartialView Test!@ViewBag.Test</div>@ViewBag.Test
View:
<p> @{Html.RenderPartial("ViewUserControl1");} @Html.Partial("ViewUserControl1") @{Html.RenderAction("ViewUserControl1");} @Html.Action("ViewUserControl1")</p>
Controller:
Controller: [childactiononly] // prevents direct calls to public actionresult viewusercontrol1 () {viewbag. test = "(Action) call"; return partialview ();}
Effect: