c# - ASP.NET call a controller method from the master page? -
in asp.net mvc2 how call controller method master page? example wanted include overview data in master:
+--------------------------------------+ | logo welcome xyz| +--------------------------------------+ | total sales month $999 | +--------------------------------------+ | home | sales | import | export (menu)| +--------------------------------------+
and have inside sales controller method:
public actionresult totalsalesthismonth() { var totalsalesmodel = salesservice.gettotalsalesthismonth() return view(totalsalesmodel); }
how can call view inside master displayed on every page?
you use html.action or html.renderaction helpers. example put following somewhere on master page:
<%= html.action("totalsalesthismonth", "somecontroller") %>
this execute controller action, render view , insert generated html @ specified location in master page. restrict action being used child action decorating [childactiononly]
attribute:
[childactiononly] public actionresult totalsalesthismonth() { var totalsalesmodel = salesservice.gettotalsalesthismonth() return view(totalsalesmodel); }
and if inside controller action wanted test whether called normal action or child action this:
public actionresult totalsalesthismonth() { var totalsalesmodel = salesservice.gettotalsalesthismonth() if (controllercontext.ischildaction) { return view("foo", totalsalesmodel); } return view("bar", totalsalesmodel); }
Comments
Post a Comment