The Best Way to Highlight or Change Current Menu Item CSS Class in MVC - asp.net-mvc

I have read a few articles as I was searching for a solution. They all seemed to favor a hard-coded or HTML Helper alternative; however, I wanted something simple and database driven. This is my best solution (submitted as an answer, by me).
Here are some other articles' solutions:
An easy way to set the active tab using controllers and a usercontrol in ASP.NET MVC?
asp.net mvc and css: Having menu tab stay highlighted on selection
ASP.NET MVC: Tabs ASCX - Adjust CSS class based on current page?

Just pass a TempData down from one of your controllers like this:
TempData("CurrentPage") = "Nutrition"
Then, in your View add a conditional like this:
<ul>
#For Each item In Model
Dim currentItem = item
If (Not String.IsNullOrEmpty(TempData("CurrentPage")) And TempData("CurrentPage") = currentItem.NAV_Element) Then
#<li><a class="current" href="#Html.DisplayFor(Function(modelItem) currentItem.NAV_Destination)">#Html.DisplayFor(Function(modelItem) currentItem.NAV_Element)</a></li>
Else
#<li>#Html.DisplayFor(Function(modelItem) currentItem.NAV_Element)</li>
End If
Next
</ul>

I have accomplished this in the past by using the route values to generate a href and then setting the parent tab to active based on that.
This is a bootstrap flavoured version:
<ul class="nav nav-tabs">
<li>Some page</li>
<li>Some other Page</li>
</ul>
<script type="text/javascript">
$(function(){
$('a[href="#Url.Action(ViewContext.RouteData.Values)"]').parents("li").addClass("active");
};
</script>
Si

I know I am too late to answer this but if you are using ASP MVC and looking for highlighting Menu items using controller and action, here is the solution which is working perfectly fine for me -
<li> <i class="fa fa-user-plus"></i> <span>Signup</span> </li>

Related

asp net core activate a controller and action with href? [duplicate]

I'm currently trying to make a website in ASP.NET Core MVC. In my layout page, I'm making a navigation bar to access all of the actions that can be reached through my controllers. I am however unable to create useful links.
<ul>
<li>Home</li>
<li>Index</li>
</ul>
My problem with this is that I still need the controller before the links and if I put the controller in front of the action like this
<ul>
<li>Home</li>
<li>What We've Done</li>
</ul>
When I click on one link and then the other, the link will end up being "myurl/home/home/page".
How can I create the link to only link to the exact page I want to?
You should use the anchor tag helper to build the markup for the link
<a asp-action="index" asp-controller="home">Home</a>
This will generate the correct relative path to the index action as the href property value of the anchor tag.

Sub menu with MvcSiteMapProvider

Currently i'm using the MvcSiteMapProvider to provide my sitemap. I'm now converting my menu to use the same XML. I have created a MyMenu.cshtml with the following code
#model MvcSiteMapProvider.Web.Html.Models.MenuHelperModel
#using MvcSiteMapProvider.Web.Html.Models
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
#foreach (var node in Model.Nodes) {
<li>#Html.DisplayFor(m => node)</li>
}
</ul>
#Html.Partial("_LoginPartial")
</div>
And i display it with
#Html.MvcSiteMap().Menu("MyMenu")
I'm trying to get nested menus to work. As i have some dropdown menu items that havesub catergories. For example
--Main Menu
------Sub Menu
What would be the best way to accomplish this?
The menu templates are broken up into 3 separate parts, so you will need to override each part (not just the main one) by specifying the additional templates inside of your custom one. See this answer for a set of templates to get you started. Then it is just a matter of modifying the HTML the way you would like.

How to create Bootstrap navigation menu using MVCSiteMap

Could you provide me step by step how to create Bootstrap 3 Menu Navigation Bar and Breadcrumb using MVCSiteMap MVC5?
I've got problem when change this code to Bootstrap model
#model MvcSiteMapProvider.Web.Html.Models.MenuHelperModel
#using System.Web.Mvc.Html
#using MvcSiteMapProvider.Web.Html.Models
<ul id="menu" class="nav navbar-nav">
#foreach (var node in Model.Nodes)
{
<li>
#Html.DisplayFor(m => node)
#if (node.Children.Any())
{
#Html.DisplayFor(m => node.Children)
}
</li>
}
</ul>
what I want is, there is BootstrapMenuHelperModel that read mvcSiteMapNode transform into navigation menu.
You are likely having issues because you have only solved part of the CSS problem. There are 3 different templates that are used by the Menu and 2 of them are also used by other HTML helpers. For that reason, it might be a good idea to create named templates (as in the example answer below) to create separate templates for each HTML helper (Menu, SiteMapPath, SiteMap, etc). For each level of nodes, the Menu recursively calls the SiteMapNodeListHelper, which might not be producing the HTML that Bootstrap is expecting (depending on which of the nav options you are using).
See this answer for a starting point, and then you can modify the HTML and class attributes from there.
Keep in mind you can also use custom attributes to supply additional data (such as CSS class names) based on the node selected, or you can use the IsCurrentNode and IsInCurrentPath properties of the SiteMapNodeModel to determine if the current node is active, if needed.
Here is a tutorial how to style your Sitemap:
Tutorial
Install Bootstrap and apply the classes to the tutorial:
Nav Bootstrap

ASP.NET MVC Razor Concatenation

I'm trying the render an HTML list that looks like the following, using the Razor view engine:
<ul>
<li id="item_1">Item 1</li>
<li id="item_2">Item 2</li>
</ul>
The code that I am attempting to use to render this list is:
<ul>
#foreach (var item in Model.TheItems)
{
<li id="item_#item.TheItemId">Item #item.TheItemId</li>
}
</ul>
The parser is choking, because it thinks that that everything to the right of the underscore in the id attribute is plain text and should not be parsed. I'm uncertain of how to instruct the parser to render TheItemId.
I don't want to but a property on the model object that includes the item_ prefix.
I also have to keep this syntax as I am using the list with JQuery Sortable and with the serialize function that requires the id attribute to be formatted in this syntax.
You should wrap the inner part of the call with ( ):
<li id="item_#(item.TheItemId)">
How about using String.Format? like this:
<li id="#String.Format("item_{0}", item.TheItemId)">
I prefer:
<li id="#String.Concat("item_", item.TheItemId)">
The verbosity tells the support developers exactly what is happening, so it's clear and easy to understand.
You can even use this way to concat more strings:
<li id="#("item-"+item.Order + "item_"+item.ShopID)" class="ui-state-default"></li>
Here is another post.
Hope helps someone.
You can so this in a simpler way:
id="item_#item.TheItemId"
This post seems to be older but now this does works now in latest MVC:
id="item_#item.TheItemId"

Jquery Tab: Can I use Html.ActionLink to fetch server data?

I'm using JQuery UI 1.7/PACKT book. It said on p.63 that the content of a page can be loaded just by using this statement
<li>AJAX Tab</li>
How can I obtain something with Html.ActionLink()
Thanks for helping.
If you are using the jQuery UI Tabs, Html.ActionLinks will work exactly the same way. The key is to ensure that you have your unordered list items within a div tag with an id of "tabs."
Something like this:
<div id="tabs">
<ul>
<li><%= Html.ActionLink("Tab Name 1", "Action1", new { Controller = "ControllerName" })%></li>
<li><%= Html.ActionLink("Tab Name 2", "Action2", new { Controller = "ControllerName" })%></li>
</ul>
</div>
Just include this within your .aspx file that you want the tabs to appear, and make sure to include the jquery and jquery-ui scripts, and it should work for you. You can read about the various Html.ActionLink parameters by following the link. That should help you understand the ActionLink better.
Hope that helps! Please let me know if you need any additional clarification.

Resources