Html.Partial vs Html.RenderPartial & Html.Action vs Html.RenderAction

后端 未结 13 780
感动是毒
感动是毒 2020-11-22 04:03

In ASP.NET MVC, what is the difference between:

  • Html.Partial and Html.RenderPartial
  • Html.Action and Html.
相关标签:
13条回答
  • 2020-11-22 04:09

    Difference is first one returns an MvcHtmlString but second (Render..) outputs straight to the response.

    0 讨论(0)
  • 2020-11-22 04:09

    More about the question:

    "When Html.RenderPartial() is called with just the name of the partial view, ASP.NET MVC will pass to the partial view the same Model and ViewData dictionary objects used by the calling view template."

    “NerdDinner” from Professional ASP.NET MVC 1.0

    0 讨论(0)
  • 2020-11-22 04:11

    For "partial" I always use it as follows:

    If there's something you need to include in a page that you need to go via the controller (like you would with an Ajax call) then use "Html.RenderPartial".

    If you have a 'static' include that isn't linked to a controller per-se and just in the 'shared' folder for example, use "HTML.partial"

    0 讨论(0)
  • 2020-11-22 04:12

    @Html.Partial and @Html.RenderPartial are used when your Partial view model is correspondence of parent model, we don't need to create any action method to call this.

    @Html.Action and @Html.RenderAction are used when your partial view model are independent from parent model, basically it is used when you want to display any widget type content on page. You must create an action method which returns a partial view result while calling the method from view.

    0 讨论(0)
  • 2020-11-22 04:12

    Partial or RenderPartial: No need to create action method. use when data to be display on the partial view is already present in model of current page.

    Action or RenderAction: Requires child action method. use when data to display on the view has independent model.

    0 讨论(0)
  • 2020-11-22 04:20

    Html.Partial returns a String. Html.RenderPartial calls Write internally and returns void.

    The basic usage is:

    // Razor syntax
    @Html.Partial("ViewName")
    @{ Html.RenderPartial("ViewName");  }
    
    // WebView syntax
    <%: Html.Partial("ViewName") %>
    <% Html.RenderPartial("ViewName"); %>
    

    In the snippet above, both calls will yield the same result.

    While one can store the output of Html.Partial in a variable or return it from a method, one cannot do this with Html.RenderPartial.

    The result will be written to the Response stream during execution/evaluation.

    This also applies to Html.Action and Html.RenderAction.

    0 讨论(0)
提交回复
热议问题