@Html. Partial, @Html. Action, @Html. renderpartial, @Html. renderaction [Go]

Source: Internet
Author: User

@Html. Action: A corresponding action is required, and the action method has a return value. (Note: You will need to return the desired value when you have finished processing the business logic)

@{html.renderaction}: There is no return value required for the corresponding Action,action method. (Note: Only business logic is processed and no return value is required)

@Html. Partial: does not need to have the action, directly takes viewname corresponding page content, has the return value. Return an object (mvchtmlstring), callback a string to send a bunch of HTML back out, and then write it into the main page; (Note: Patial's data comes from the called view)

@{html.renderpartial}: do not need to have an action, directly get viewname corresponding page content, no return value. The callback is void, and this method adds the specified view on the main page; (Note: The renderpatial data comes from the called view)

**********************************************************************************

@Html. partial/@{htmt.renderpartial ()}@Html. Partial to render a partial view as a string @{html.renderpartial} writes the distribution view directly to the response output stream, so it can only be placed directly in the code block, not in the expression (the return value is void) RenderPartial because it is written directly in the response stream, the performance is better (micro-impact), and the partial is not written in the code block, so it is more convenient @Html. Action ()/@Html. Renderaction ()and partial are similar to renderpartial, but because they pass action, they are more flexible and can take advantage of the controller context. In action, you can use the ischildaction to determine whether it is called directly by the URL or by the action ()/renderaction () call to take advantage of the overloaded direct arguments to the action. Renderaction preferred to use Actionnameattribute. Use return Partialview () in the action to specify the partial view, and the layout specified in _viewstatrt.cshtml will not be valid. ***********************************************************************************

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.

renderpartial and RenderactionThe same point of the two

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 Renderaction comes from itself.
    2. Renderaction will initiate a new request, and renderpatial will not.

Html.renderaction calls the MVC user control through an action in the controller

This method renderpartial a bit different, he is directly re-execute the order of Controller→model→view, and then bring the resulting page back to the original view and then return.

The parameters passed in Html.renderaction are the Controller and action names that are executed. If a method in the controller can only be called by Html.renderaction (), you can add an attribute on the method [Childactiononly], which is inaccessible elsewhere. The main purpose of childactiononly is to let this action not be accessed directly by entering an address in the address bar, but by calling it through Renderaction;

ActionName means defining a new name for the action.

[ActionName ("Newtest")] so modified, then the call is not test, but newtest;

 Public class childactiononlytestcontroller:controller{[childactiononly]// Prevent direct invocation
Public ActionResult getsupplierlist () { varnew Suppliercontroller (); return Controller. Supplierlist (); }}

The following is a comparison chart of the execution process

An example:
Controller:

// article menu list [Childactiononly]  Public  var item = _db. Classes.tolist ();   return Partialview (item);}

View:Articlemenu.cshtml

@model ienumerable<class>@foreach (varin"article  "nullnew"tree_children""MainFrame "  })}

ADD View:Index.cshtml

@{html.renderaction ("articlemenu""article");}

A second example:
Controller: Note that part of the view in Partialview is not model

[Childactiononly]  Public ActionResult cartsummary () {var cart = Shoppingcart.getcart (this. HttpContext); viewdata["cartcount"] =return partialview ("  Cartsummary");}

PartiaView:CartSummary.cshtml

 @Html. ActionLink ( " cart (  " + viewdata["  cartcount   "] + "  )   " ,  "      " shoppingcart  "  ,  new  {id =  " cart-status  }) 

ADD View:

@{html.renderaction ("genremenu" "Store");}

make a summary of these four differences, clean up the idea, convenient for later use:1, the return value of the method with Render is void, the output is inside the method, and the return value type is mvchtmlstring, so this can only be used: @Html. Partial corresponds to @{html.renderpartial (...);} @Html. Action corresponds to @{html.renderaction (...);}2, Action, renderaction Load method View, execute Controller→model→view order, and then return the resulting page back to the original view. Partial and renderpartial directly load the contents of the view file3. 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, Html.partial is recommended for simple user controls without any logic, and Html.action is recommended for user controls that need to be set up with some model. Of course, there are model data can also use the Html.partial method, you can see the overloads of the method. 5, the advantage of using html.action is that you can choose different user controls based on different scenarios. For example: @Html. Action ("Userinfocontrol"In the corresponding Userinfocontrol action, when the user is not logged in, you can Retun Partialview ("Logonusercontrol"); After logging in, you can Retun Partialview ("Userinfocontrol");
Summary of differences

Ext.: http://www.cnblogs.com/firstcsharp/p/3142091.html

Http://www.cnblogs.com/gesenkof99/archive/2013/06/03/3115052.html

@Html. Partial, @Html. Action, @Html. renderpartial, @Html. renderaction [Go]

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.