Html.Partial,Html.RenderPartial,Html.Action and Html.RenderAction in ASP.NET MVC

Html.Partial and Html.RenderPartial     

  The Html.Partial helpers renders a partial view in to string.You can reuse this Partial Views in Multiple Views. Partial has four overloads
public void Partial(string partialViewName);

public void Partial(string partialViewName, object model);

public void Partial(string partialViewName, ViewDataDictionary viewData);
public void Partial(string partialViewName, object model,ViewDataDictionary viewData);
 Example : 
 @Html.Partial("MyPartial")
 
Difference Between Render Partial and Partial

RenderPartial writes directly to the response output stream instead of returning a string. So, you should place RenderPartial inside a code block instead of a code expression

Example :
 @{Html.RenderPartial("MyRenderPartial");}

 So, which should you use, Partial or RenderPartial?

In general, you should prefer Partial to RenderPartial because Partial is more convenient (you don’t have to wrap the call in a code block with curly braces). However, RenderPartial may result in better performance because it writes directly to the response stream.

Html.Action and Html.RenderAction

         Html.Action and Html.RenderAction are similar to Partial and RenderPartial.Action  executes a separate controller action and display the results.the only difference between Action and RenderAction is that RenderAction writes directly to the response.

 Example :
Imagine you are using the following controller
public class ActionDemoController : Controller {
public ActionResult Index() {
return View();
}

[ChildActionOnly]
public ActionResult DisplayMenu() {
var menu = GetMenuFromSomewhere();
return PartialView(menu);
}
}

The Menu action builds a menu model and returns a partial view with just the menu:
@model Menu
<ul>
@foreach (var item in Model.MenuItem) {
<li>@item.Text</li>
}
</ul>
In your Index.cshtml view, you can now call into the Menu action to display the menu:
<html>
<head><title>Index with Menu</title></head>
<body>
@Html.Action("DisplayMenu")
<h1>Welcome to the Index View</h1>
</body>
</html>

Notice that the Menu action is marked with a ChildActionOnlyAttribute. The attribute prevents the runtime from invoking the action directly via a URL. Instead, only a call to Action or RenderAction can invoke a child action. The ChildActionOnlyAttribute isn’t required, but is generally recommended for child actions.
Passing values to Action
       Sometimes you want to pass parameters in to the action.
Example:
[ChildActionOnly]
public ActionResult Menu(MenuParameters param) {
return PartialView(
param);

}

@Html.Action(“Menu”, new {options = new MenuParameters { Width=400, Height=500 } })

Enjoy Coding…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s