Ich habe menu items on master page
und wenn Benutzer auf diesen Menüpunkt klicken, möchte ich es aktiv zeigen. Ich möchte Active an vorhandene Klasse anhängen, um sie aktiv zu machen.Wie wird die aktive Klasse an die angeklickte Menüoption angefügt?
_Layout.cshtml:
<div class="menu-items-navigation">
<div class="Classy-item" class="@Html.IsSelected(actions: "Index", controllers: "Classy")">
<a href="@Url.Action("Index", "Classy")" >
<div style="padding-top: 50px;">Classy Items</div>
</a>
</div>
<div class="Regular-stuff">
<a href="@Url.Action("Index", "RegularStuff")">
<div style="padding-top: 50px;">Regular Stuff</div>
</a>
</div>
<div class="Popular-Vessels">
<a href="@Url.Action("Index", "PopularVessels")">
<div style="padding-top: 50px;">Popular Vessels</div>
</a>
</div>
</div>
Nun, wenn ich diesen Menüpunkt wollen Active
sein Ich habe 1 Klasse als aktiv bezeichnet, in denen möchte ich dies geben, Div, klicken, wenn der Benutzer dies jede 3 Menüpunkte.
Zum Beispiel, wenn ich Regular Stuff as Active when user clicks
drauf machen will, dann wird es so sein:
<div class="Regular-stuff Active">
Wenn Benutzer klicken Sie auf Classy Artikel dann:
<div class="Classy-item Active">
-Code von hier genommen Reference:
public static string IsSelected(this HtmlHelper html, string controllers = "", string actions = "", string cssClass = "Active")
{
ViewContext viewContext = html.ViewContext;
bool isChildAction = viewContext.Controller.ControllerContext.IsChildAction;
if (isChildAction)
viewContext = html.ViewContext.ParentActionViewContext;
RouteValueDictionary routeValues = viewContext.RouteData.Values;
string currentAction = routeValues["action"].ToString();
string currentController = routeValues["controller"].ToString();
if (String.IsNullOrEmpty(actions))
actions = currentAction;
if (String.IsNullOrEmpty(controllers))
controllers = currentController;
string[] acceptedActions = actions.Trim().Split(',').Distinct().ToArray();
string[] acceptedControllers = controllers.Trim().Split(',').Distinct().ToArray();
return acceptedActions.Contains(currentAction) && acceptedControllers.Contains(currentController) ?
cssClass : String.Empty;
}
Aber ich bin hier verwirrt, wie zu klassifizieren Diese Isselected-Methode wird beim Klicken auf bestimmte Menüelemente angeklickt, da ich die Klassenattribute bei div nicht zweimal verwenden kann.
Es funktioniert. Vielen Dank. Denken Sie daran. –