views:

19

answers:

1

i have master mage, in bottom i have such menu

<div class="footMenu">
            <ul>
                <li class="active"><a href="">test1</a> </li>
                <li><a href="">test2</a></li>
                <li><a href="">test3</a></li>               
            </ul>
        </div>

so in depends of view load a need to put class="active" in current li item. method test1() so active link test1,method test2() so active link test2. how can it be done?

+1  A: 

In a asp.net mvc application I usually solve this by setting a id of the body tag that contains both the name of the controller and the name of the action. Then I use css to style links differently depending on the id of the body tag.

I usually do this by creating a html helper extension that gets the id string for the body tag. That can look something like this:

public static string BodyId(this HtmlHelper helper) {
    var controllerName = helper.ViewContext.RouteData.GetRequiredString("controller").ToLower();
    var actionName = helper.ViewContext.RouteData.GetRequiredString("action").ToLower();

    return string.Format("{0}-{1}", controllerName, actionName);
}

Then use it like this:

<body id="<%=Html.BodyId()%>">

Then you can put ids of all your links as well. To make it clearer they can also contain the name of the controller and the name of the action that they should link to. Something like this:

<ul>
    <li><a href="" id="home-index-link">test1</a></li>
    <li><a href="" id="account-login-link">test2</a></li>       
</ul>

Then I use css to style them differently depending on if they are selected or not. Something like this:

#home-index #home-index-link, #account-login #account-login-link {
    /* Styles for selected links */
}

It will, of course, work if you do the same for the <li /> tag instead of the <a /> tag.

If you don't want to do it like that you can of course just use the routedata (as I do in my html helper here). The value with the key "action" will contain the name of the action and the value with the key "controller" will contain the name of the controller.

Mattias Jakobsson
very hard solution. i guess it will be like this<li <% if (method == "somemethod") {%> class="active"<%}%>><a href="">Магазин фасадов</a> </li>
kusanagi
Yes. Thats why I do it as I explained. To avoid if statements. But if you want to write if statements then this: ViewContext.RouteData.GetRequiredString("action") (as I explained in the end of my answer) will contain the name of the action you are executing.
Mattias Jakobsson