random techno tidbits
Razor: Html.RenderPartial vs Html.Partial, Html.RenderAction vs Html.Action – what one should use?
This question can be confusing for people starting using ASP.NET MVC3.
Today, we have two ways to render partial and action stuff:
@Html.Partial("Details") @Html.Action("Index", "Home")
and
@{ Html.RenderPartial("Details"); } @{ Html.RenderAction("Index", "Home"); }
So what would you go with?
First approach looks cleaner, but I would use RenderPartial and RenderAction in most cases, just because these methods output their content into the same TextWriter object as used in the current template. In contrast to this, @Html.Partial and @Html.Action methods create their own TextWriter instances each time and buffer all their content into memory. In the final stage they just emit all writer content it into a plain MvcString object, so that it can be rendered as an unescaped HTML by the @ directive.
On the other side, I do not entirely through away @Html.Partial and @Html.Action. These methods are good for some cases when you need to put sub-template content into a variable or do something with it (which was a pain in older MVC versions). And they may be life-saving in certain scenarios like caching.
Oh, there is also a @RenderPage method of WebPageBase that doesn’t use MVC template lookup and receives exact template path as its parameter. But anyway I would use HtmlHelper’s Partial instead, as it is a more traditional view-engine independent approach for rendering child templates.
-
Ray247
-
http://pratapreddypilaka.blogspot.com/ Pratap Reddy