简体   繁体   中英

ASP.NET call a controller method from the master page?

In ASP.NET MVC2 how do you call a controller method from the master page? Say for example I wanted to include some overview data in the master:

+--------------------------------------+
| Logo                      Welcome xyz|
+--------------------------------------+
| total sales this month $999          |
+--------------------------------------+
| Home | Sales | Import | Export (menu)|
+--------------------------------------+

And I have inside the Sales controller this method:

public ActionResult TotalSalesThisMonth()
{
    var totalSalesModel = SalesService.GetTotalSalesThisMonth()
    return View(totalSalesModel);
}

How can I call that View from inside the master so that it will be displayed on every page?

You could use the Html.Action or Html.RenderAction helpers. For example you could put the following somewhere on your master page:

<%= Html.Action("TotalSalesThisMonth", "SomeController") %>

This will execute the controller action, render the view and insert the generated HTML at the specified location in the master page. You could also restrict this action for being used only as child action by decorating it with the [ChildActionOnly] attribute:

[ChildActionOnly]
public ActionResult TotalSalesThisMonth()
{
    var totalSalesModel = SalesService.GetTotalSalesThisMonth()
    return View(totalSalesModel);
}

And finally if inside the controller action you wanted to test whether it was called as a normal action or as a child action you could do this:

public ActionResult TotalSalesThisMonth()
{
    var totalSalesModel = SalesService.GetTotalSalesThisMonth()
    if (ControllerContext.IsChildAction)
    {
        return View("foo", totalSalesModel);
    }
    return View("bar", totalSalesModel);
}

Quoting from http://haacked.com/archive/2009/11/18/aspnetmvc2-render-action.aspx

I'll use the term RenderAction to refer to both of these methods. Here's a quick look at how you might use this method. Suppose you have the following controller.

public class MyController {
  public ActionResult Index() {
    return View();
  }

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

The Menu action grabs the Menu model and returns a partial view with just the menu.

<%@ Control Inherits="System.Web.Mvc.ViewUserControl<Menu>" %>
<ul>
<% foreach(var item in Model.MenuItem) { %>
  <li><%= item %></li>
<% } %>
</ul>

In your Index.aspx view, you can now call into the Menu action to display the menu:

<%@ Page %>
<html>
<head><title></title></head>
<body>
  <%= Html.Action("Menu") %>
  <h1>Welcome to the Index View</h1>
</body>
</html>

The content above is licensed under CC-BY: http://creativecommons.org/licenses/by/2.5/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM