Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Html.Partial or Html.RenderPartial with MVC3?

I am totally confused even after seeing the following explanation.

<div>
  @Html.Partial("_FeaturedProduct")
</div>

Partial views can be rendered inside a Layout Page (or if using MVC 2/3 w/ASPX, the Master Page) as well as regular views.

There are some cases where you might like to step aside and write directly to the HTTP Response stream rather than having a partial view render the results (partials/views use MvcHtmlString/StringWriter). To do so, use the Html.RenderPartial helper.

<div>
  @Html.RenderPartial("_FeaturedProduct")
</div>

Can someone tell me what it means? What cases where I might like to write direct to the HTTP Response etc. What if my partial view contains just one line like this:

<h1>Hello</h1>

Which should I use and why? What would happen if I used the other?

The following confused me even more: "Use Html.RenderPartial for streaming images or other elements that are media-centric or where faster download times are highly important."

like image 892
Rubin Avatar asked Sep 10 '11 11:09

Rubin


People also ask

What is difference between HTML partial and HTML RenderPartial?

The primary difference between the two methods is that Partial generates the HTML from the View and returns it to the View to be incorporated into the page. RenderPartial, on the other hand, doesn't return anything and, instead, adds its HTML directly to the Response object's output.

What is an HTML partial?

Html. Partial. 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.

When should partial views be used?

Partial views are an effective way to: Break up large markup files into smaller components. In a large, complex markup file composed of several logical pieces, there's an advantage to working with each piece isolated into a partial view.

What is the difference between RenderPartial and render action?

RenderPartial is used to display a reusable part of within the same controller and RenderAction render an action from any controller. They both render the Html and doesn't provide a String for output.


1 Answers

See the response below.

The only difference is that Partial returns an MvcHtmlString, and must be called inside <%= %>, whereas RenderPartial returnsvoid and renders directly to the view.

If you look at the source code, you'll see that they both call the same internal method, passing a StringWriter for it to render to.

You would call Partial if you want to view, save, or manipulate the generated HTML instead of writing it to the page.

What is the difference (if any) between Html.Partial(view, model) and Html.RenderPartial(view,model) in MVC2?

like image 190
LiamB Avatar answered Oct 01 '22 02:10

LiamB