Adición condicional de clase basada en la ruta
Frecuentes
Visto 82 veces
0
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:
<ol>
<li <% if (action.Equals("Subnav1")) { %><%: class="selected" %><% } %>>Subnav 1</li>
<li <% if (action.Equals("Subnav2")) { %><%: class="selected" %><% } %>>Subnav 2</li>
</ol>
1 Respuestas
1
<ol>
<% 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>
</ol>
Respondido el 09 de Septiembre de 13 a las 21:09
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas asp.net-mvc asp.net-mvc-4 master-pages or haz tu propia pregunta.
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