asp.net mvc 4 - C# MVC Get Current View/Dynamic Template -


i trying return current dynamic view allow me append css class actionlink if current view same actionlink.

as passing majority of links through specific route, in case pages, currentaction pages in cases, despite actual view or template being returned actionresult called.

so example if url http://mytestdomain.com/sport currentaction sport , not pages.

please see code below:

routeconfig.cs

routes.maproute("pages", "{maincategory}/{subcategory}/{pagename}", new { controller = "home", action = "pages", subcategory = urlparameter.optional, pagename = urlparameter.optional }); 

homecontroller

public static mvchtmlstring menulink(this htmlhelper htmlhelper, string linktext, string actionname, string controllername) {     var currentcontroller = htmlhelper.viewcontext.parentactionviewcontext.routedata.getrequiredstring("controller");     var currentaction = htmlhelper.viewcontext.parentactionviewcontext.routedata.getrequiredstring("action");     var currentview = htmlhelper.currentviewname();      var builder = new tagbuilder("li")     {         innerhtml = htmlhelper.actionlink(linktext, actionname, controllername).tohtmlstring()     };      builder.addcssclass("dropdown");      var actionsplit = actionname.trimstart('/').split('/');      actionname = actionsplit[0];      if (controllername == currentcontroller && actionname == currentaction)     {         return new mvchtmlstring(builder.tostring().replace("a href", "a class=\"active\" href").replace("</li>", "").replace("home/", ""));     }      return new mvchtmlstring(builder.tostring().replace("</li>", "").replace("home/", "")); }  public static string currentviewname(this htmlhelper html) {         return system.io.path.getfilenamewithoutextension(((razorview)html.viewcontext.view).viewpath);     }  public actionresult pages(string maincategory, string subcategory, string pagename) {     if (!string.isnullorempty(pagename))     {         subcategory = subcategory + "/" + pagename;     }      page model;      using (cmsentities)     {         model = (from f in cmsentities.getpage(1, maincategory, subcategory, "live") select f).firstordefault();     }              return view(model.template, model); } 

navigation.cshtml

@html.menulink(navigation.title, "/" + html.tofriendlyurl(navigation.title), "home") 

i have tried using var currentview = htmlhelper.currentviewname(); return navigation actionlink being called within [childactiononly] public actionresult navigation() example @{ html.renderaction("navigation", "home"); } within views/shared/_layout.cshtml

any appreciated :-)

in end used 'httpcontext.current.request.url.absolutepath' determine current location append active class matching page link.

public static mvchtmlstring menulink(this htmlhelper htmlhelper, string linktext, string actionname, string controllername) {     var currentcontroller = htmlhelper.viewcontext.parentactionviewcontext.routedata.getrequiredstring("controller");     var currenturl = httpcontext.current.request.url.absolutepath.trimstart('/').split('/');      var maincategory = currenturl[0];      var builder = new tagbuilder("li")     {         innerhtml = htmlhelper.actionlink(linktext, actionname, controllername).tohtmlstring()     };      builder.addcssclass("dropdown");      var actionsplit = actionname.trimstart('/').split('/');      actionname = actionsplit[0];      if (actionsplit.length == 1)     {         if (controllername == currentcontroller && actionname == maincategory)         {             return new mvchtmlstring(builder.tostring().replace("a href", "a class=\"active\" href").replace("</li>", "").replace("home/", ""));         }     }      return new mvchtmlstring(builder.tostring().replace("</li>", "").replace("home/", "")); } 

i hope proves useful others :-)


Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -