Difference in Html.Partial vs Html.RenderPartial & Html.Action vs Html.RenderAction?


I would like to know difference between

  1. HTML.Partial and HTML.RenderPartial
  2. Html.Action and Html.RenderAction

in asp.net MVC, when we should use which HTML helper method?

Thanks


Asked by:- neena
1
: 6630 At:- 7/18/2017 3:46:51 PM
asp.net mvc C# partial views renderaction renderpartial







2 Answers
profileImage Answered by:- pika

So here is the difference between HTML.Partial and HTML.RenderPatial

HTML.Partial

  • It Renders the partial view as an HTML-encoded string.

  • This method result can be stored in a variable since it returns MvcHtmlString value(which can be assigned to a variable and manipulate it if required).

  • You don't need to create any action, you can directly call the partial view, and render it's results, with model or without passing any model.

  • Syntax:
    @Html.Partial("_PartialViewName")?

HTML.RenderPatial

  • This method results will be directly written to the HTTP response stream means it uses the same TextWriter object as used in the current web page/template.

  • This method also returns void.

  • Use RenderPartial when you have a model to send to the view and there will be a lot of HTML that doesn't need to be stored in a variable.

  • No Need to create any action

  • Syntax
    @{Html.RenderPartial("_ViewName");}?
  • This method is the best choice when you want to cache a partial view.

Html.Action

  • It returns a string.
  • Syntax:
    @Html.Action("Action","Controller")
  • For this method, we need to create a child action for the rendering the partial view.
  • Its result can be manipulated before rendering to the output stream.
  • Manipulation : @{ var data= Html.Action(“Action”,”Controller”, new {ParameterName=ParameterValue}); } @data I am Software Developer.
  • Use when data length is small or need to manipulate result before rendering to the output stream, Action method is useful when the displaying data in the partial view is independent of corresponding view model

Html.RenderAction

  • It returns void.
  • Syntax:
    @{ Html.RenderAction ("Action","Controller"); }?
  • It is faster as compared to @Html.Action because this result is written directly to the output stream.
  • It’s result can not be manipulated.
  • Use RenderAction when you do not have a model to send to the view and have a lot of html to bring back that doesn't need to be stored in a variable.
2
At:- 7/19/2017 7:50:10 AM Updated at:- 7/19/2017 7:50:54 AM


profileImage Answered by:- Vinnu

Html.Partial

  • Syntax : @Html.Partial("PartialViewName", new { id = 1} );
  • Renders the partial view as an HTML-encoded string.
  • This method result can be stored in a variable, since it returns string type value.
  • Simple to use and no need to create any action.

Html.RenderPartial

  • Syntax @{ Html.RenderPartial("PartialViewName", ModelObject);}
  • This method result will be directly written to the HTTP response stream means it uses the same TextWriter object as used in the current webpage/template
  • This method returns void
  • This method is faster than Partial method since its result is directly written to the response stream which makes it fast

HTML.RenderAction?? Vs HTML.Action

Both are action helper methods in ASP.NET MVC. Generally both methods are used for calling action methods or child action methods and rendering the result of the action method in the view.

  • @Html.Action(“ActionName”, “ControllerName”) – returns a String as an output
  • @{ Html.RenderAction(“ActionName”, “ControllerName”); } –  returns void as an output
1
At:- 8/2/2018 6:59:08 AM Updated at:- 8/2/2018 7:02:38 AM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use