Differences and usage of HTML. Partial, renderpartial, action, and renderaction in Asp.net MVC

Source: Internet
Author: User

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
  1. Renderpatial data comes from the called view, while renderaction comes from itself.
  2. 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:

 

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.