Asp.net MVC in Html.Partial, RenderPartial, Action, RenderAction differences and usage

Partial and RenderPartial:
These two properties are the same, only refers to a set in a View to come in, but the return value is a little different from
a return of Object Partial (MvcHtmlString), return a String to a pile of Html return out, then written to main page

@Html.Partial("ViewName")

RenderPartial return is void, and this method adds the specified View the main page

@{
            Html.RenderPartial("ViewName");
    } 

These two methods did not use Controller, directly to a View (Page) to add to the mix
and RenderAction a bit different, this is a Action, it will be used later to return to pass a page Controller

public class ChildActionOnlyTestController : Controller

{

  [ChildActionOnly]

  public ActionResult GetSupplierList()

  {

    var controller = new SupplierController();

    return controller.SupplierList();

  }

}

 

RenderPartial RenderAction and the same two points

It is commonly used to display a relatively independent function of "block", for example, to display the menu, or a navigation bar. The results are displayed on both of output of the call as part of View.

The differences between the two

  1. RenderPatial data from the View calls, and RenderAction from himself.
  2. RenderAction will launch a new Request, and RenderPatial not.

 

 

@Html.Partial/@{Htmt.RenderPartial()}
@ Html.Partial for rendering a partial view of the string
@ {Html.RenderPartial} view written directly to the distribution in response to the output stream, the code blocks can not be directly placed, can not be placed in the expression (return value is void)
 
RenderPartial because it is written directly in the response stream, the better performance (micro-impact), but without Partial writing the code block, it is more convenient
 
@Html.Action()/@Html.RenderAction()
Partial RenderPartial and with similar, but because the Action will pass, so more flexible, context controller may be utilized.
In the Action can be determined by IsChildAction is called directly or be Url Action () / RenderAction () call
Overloaded parameters can be used directly to Action.
RenderAction precedence ActionNameAttribute.
Action use return PartialView () specified partial view, specified in the Layout _ViewStatrt.cshtml become invalid.
 
4 kinds PartialView comparison:
PartialView:
<div>Just a PartialView [email protected]</div>
@ViewBag.Test

 

View:
<p>
    @{Html.RenderPartial("ViewUserControl1");}
    @Html.Partial("ViewUserControl1")
    @{Html.RenderAction("ViewUserControl1");}
    @Html.Action("ViewUserControl1")
</p>

 

Controller:
Copy the code
Controller:
[ChildActionOnly] // prevent direct call
public ActionResult ViewUserControl1()
{
    ViewBag.Test = "(Action) calls";
    return PartialView();
}
Copy the code

 

        effect:
    

     

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

Guess you like

Origin www.cnblogs.com/Jacob-Wu/p/12041684.html