Html.Action and Html.RenderAction in MVC

Action and RenderAction are similar to the Partial and RenderPartial helpers. The Partial helper typically helps a view render a portion of a view’s model using view markup in a separate file.
Action, on the other hand, executes a separate controller action and displays the results. Action offers more fl exibility and re-use because the controller action can build a different model and make use of a separate controller context.
Once again, the only difference between Action and RenderAction is that RenderAction writes directly to the response (which can bring a slight effi ciency gain). Here’s a quick look at how you might use this method. Imagine you are using the following controller:

public class MyController : Controller
{
public ActionResult Index()
{
return View();
}
[ChildActionOnly]
public ActionResult Menu()
{
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("Menu")
<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.
Since MVC 3 there is also a new property on the ControllerContext named IsChildAction. IsChildAction will be true when someone calls an action via Action or RenderAction (but false when invoked through a URL). Some of the action fi lters of the MVC runtime behave differently with child actions (such as the AuthorizeAttribute and OutputCacheAttribute).

Passing Values to RenderAction

Because these action helpers invoke action methods, it’s possible to specify additional values to the target action as parameters. For example, suppose you want to supply the menu with options.

  1. You can defi ne a new class, MenuOptions, as follows:
    public class MenuOptions {
    public int Width { get; set; }
    public int Height { get; set; }
    }
  2. Change the Menu action method to accept this as a parameter:
    [ChildActionOnly]
    public ActionResult Menu(MenuOptions options)
    {
    return PartialView(options);
    }
  3. You can pass in menu options from your action call in the view:
    @Html.Action("Menu", new { options = new MenuOptions { Width=400, Height=500 } })

Cooperating with the ActionName Attribute

Another thing to note is that RenderAction honors the ActionName attribute when calling an action name. If you annotate the action as follows, you’ll need to make sure to use CoolMenu as the action name and not Menu when calling RenderAction:

[ChildActionOnly]
[ActionName("CoolMenu")]
public ActionResult Menu(MenuOptions options)
{
return PartialView(options);
}
Tagged . Bookmark the permalink.

Leave a Reply