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
: 218 At:- 7/18/2017 3:46:51 PM
asp.net mvc C# partial views renderaction renderpartial






1 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.
0
At:- 7/19/2017 7:50:10 AM Updated at:- 7/19/2017 7:50:54 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
Amazon Deals

Jobs from Indeed