Asked  7 Months ago    Answers:  2   Viewed   108 times

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

  • Html.Partial and Html.RenderPartial
  • Html.Action and Html.RenderAction



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

The basic usage is:

// Razor syntax
@{ 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.

Tuesday, June 1, 2021
answered 7 Months ago

Html.Action invokes the controller's action, which means it instantiates the controller entity, calls an action method, which builds a model an returns a view result.

Html.Partial uses already created model (or can be called without model at all) to render a specified view.

When to use one over the other? If you already have a model and just want to have a reusable view, opt to Html.Partial. If you see that some piece deserves its own model and action, maybe it makes sense to use Html.Action.

This question is discussed in much greater details in this article, and what you see above is basically an excerpt from it.

Monday, August 16, 2021
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :