asp.net mvc - C# mvc get active menu item with menu items from partial controller -
i'm building new website in mvc4 , i'm retrieving menu dynamic database. prevent doing same actions on , on again in every controller , view create menu i'm using partialcontroller partial view build menu. i've done this article.
the next thing want achieve determine current active menu item based on current controller , action, i've created html helper this. code based on this article.
my code looks this;
partialcontroller.cs
[childactiononly] public actionresult mainmenu() { var viewmodel = new menumodel(); try { ilist<menuitem> menuitems = menuservice.first(x => x.name == "mainmenu").menuitems; //get menuitems collection somewhere if (menuitems != null && menuitems.count > 0) { viewmodel.menuitems = menuitems; return view(viewmodel); } } catch (exception exception) { //todo: exception handling , logging } return view(viewmodel); }
mainmenu.cshtml
this partial view included in layout, , among others contains piece of code;
<nav class="nav-collapse collapse"> <ul class="row-fluid nav"> @foreach (var item in model.menuitems) { <li class="span2"> @html.menuitem(item.name, item.actionname, item.controllername) </li> } </ul> </nav>
htmlhelper.cs
the htmlhelper method i'm using in partial view build menu items. 1 problem is.
public static mvchtmlstring menuitem(this htmlhelper htmlhelper, string linktext, string actionname, string controllername) { string currentaction = htmlhelper.viewcontext.routedata.getrequiredstring("action"); string currentcontroller = htmlhelper.viewcontext.routedata.getrequiredstring("controller"); if (actionname == currentaction && controllername == currentcontroller) { return htmlhelper.actionlink( linktext, actionname, controllername, null, new { @class = "active" }); } //var link = htmlhelper.actionlink("<span>{linktext}<span>", actionname, controllername).tohtmlstring(); //return new mvchtmlstring(link.replace("{linktext}", linktext)); return htmlhelper.actionlink(linktext, actionname, controllername); }
when debugging see variable 'currentcontroller' contains value 'partial' , variable 'currentaction' contains 'mainmenu'. seems quite logic me since controller action used build menu. however, issue. 2 variables contain values 'partial' , 'mainmenu' because menuitem helper called in view instantiated partialcontroller. way i'm never able match actual controller making request.
all want is, example click contact-button in menu , see active menu item. action menu-item index action of contactcontroller, if controller , action match controller , action mapped menu-item, additional css-class should added.
what can solve this? prefer in code instead of dirty javascript functions, don't know if it's possible or how it?
i think can access parent controller using parentactionviewcontext property on htmlhelper.viewcontext
string currentcontroller = htmlhelper.viewcontext.parentactionviewcontext.routedata.getrequiredstring("controller");
Comments
Post a Comment