Adición condicional de clase basada en la ruta

I have different sections to my site. Let's say one is called Accounts.

Accounts has a list of subnavigation links. Each subnavigation link has it's own view under an Accounts view folder (Views/Accounts/Subnav1, Views/Accounts/Subnav2, etc). I need to store the Account subnavigation HTML code in a single spot and have it conditionally add a class of "selected" to the appropriate tag in the HTML code.

I don't know if this is best accomplished by having an Accounts nested master page (that has the Accounts subnav) or by creating an Account subnav partial. Either way, I need to figure out how to do this in general.

Here is some pseudo-code of what I'm thinking:

Account subnav nested master page/partial:

    <li <% if (action.Equals("Subnav1")) { %><%: class="selected" %><% } %>>Subnav 1</li>
    <li <% if (action.Equals("Subnav2")) { %><%: class="selected" %><% } %>>Subnav 2</li>

preguntado el 09 de septiembre de 13 a las 21:09

1 Respuestas

    <% string action = ViewContext.Controller.ValueProvider.GetValue("action").RawValue; %>
    <li <% if (action.Equals("Subnav1", StringComparison.OrdinalIgnoreCase)) { %><%: class="selected" %><% } %>>Subnav 1</li>
    <li <% if (action.Equals("Subnav2", StringComparison.OrdinalIgnoreCase)) { %><%: class="selected" %><% } %>>Subnav 2</li>

Respondido el 09 de Septiembre de 13 a las 21:09

thank you for the fast response. would you advise accomplishing this with a partial or nested master page? - tau

@tau I'd probably throw that li into a Partial View if there are too many of them. - Yuri Faktorovich

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.