How to visually indicate current page in ASP.NET MVC?
The easiest way is to get the current controller and action from the ViewContext's RouteData. Note the change in signature and use of @ to escape the keyword.
<% var controller = ViewContext.RouteData.Values["controller"] as string ?? "Home";
var action = ViewContext.RouteData.Values["action"] as string ?? "Index";
var page = (controller + ":" + action).ToLower();
%>
<%= Html.ActionLink( "About", "About", "Home", null,
new { @class = page == "home:about" ? "current" : "" ) %>
<%= Html.ActionLink( "Home", "Index", "Home", null,
new { @class = page == "home:index" ? "current" : "" ) %>
Note that you could combine this an HtmlHelper extension like @Jon's and make it cleaner.
<%= Html.MenuLink( "About", "About", "Home", null, null, "current" ) %>
Where MenuActionLink is
public static class MenuHelperExtensions
{
public static string MenuLink( this HtmlHelper helper,
string text,
string action,
string controller,
object routeValues,
object htmlAttributes,
string currentClass )
{
RouteValueDictionary attributes = new RouteValueDictionary( htmlAttributes );
string currentController = helper.ViewContext.RouteData.Values["controller"] as string ?? "home";
string currentAction = helper.ViewContext.RouteData.Values["action"] as string ?? "index";
string page = string.Format( "{0}:{1}", currentController, currentAction ).ToLower();
string thisPage = string.Format( "{0}:{1}", controller, action ).ToLower();
attributes["class"] = (page == thisPage) ? currentClass : "";
return helper.ActionLink( text, action, controller, new RouteValueDictionary( routeValues ), attributes );
}
}
Asp.Net Mvc highlighting current page link technique?
The best way to handle this is to write an HTML helper. You could use RouteData.Values["action"]
to get the currently executing action and compare to the menu name and if they match apply a CSS class that will highlight it.
public static MvcHtmlString MenuItem(
this HtmlHelper htmlHelper,
string action,
string text
)
{
var menu = new TagBuilder("div");
var currentAction = (string)htmlHelper.ViewContext.RouteData.Values["action"];
if (string.Equals(
currentAction,
action,
StringComparison.CurrentCultureIgnoreCase)
)
{
menu.AddCssClass("highlight");
}
menu.SetInnerText(text);
return MvcHtmlString.Create(menu.ToString());
}
And then use this helper to render the menu items:
<%: Html.MenuItem("about", "About us") %>
<%: Html.MenuItem("contact", "Contact us") %>
...
ASP.NET MVC 5 - Get current view's name (Razor .cshtml side)
Since you are using the convention where your page is named after the controller you can do this in razor to get the controller/page name:
@{
var pageName = ViewContext.RouteData.Values["controller"].ToString();
}
<ul class="sidebar bg-grayDark">
<li class="@(pageName == "Home" ? "active" : "")">
<a href="@Url.Action("Index", "Home")">
<span class="mif-home icon"></span>
<span class="title">Home</span>
</a>
</li>
<li class="bg-hover-black @(pageName == "Product" ? "active" : "")">
<a href="@Url.Action("Index", "Product")">
<span class="mif-shop icon"></span>
<span class="title">Products</span>
<span class="counter">14</span>
</a>
</li>
<li class="bg-hover-black @(pageName == "Category" ? "active" : "")">
<a href="@Url.Action("Index", "Category")">
<span class="mif-flow-cascade icon"></span>
<span class="title">Categories</span>
<span class="counter">9</span>
</a>
</li>
<li class="bg-hover-black @(pageName == "User" ? "active" : "")">
<a href="@Url.Action("Index", "User")">
<span class="mif-users icon"></span>
<span class="title">Users</span>
<span class="counter">1</span>
</a>
</li>
</ul>
This will set your active class on the page server side removing the need to do this client side with javascript.
Debug current page in ASP.NET MVC
If you are using the MVC framework you are most likely using the routing system.
To specify your starting page you need to give your project the path you want to open and not the view name (and selecting "current page" doesn't work because Visual Studio has no idea what route can be used to display the view your are on).
You need to manually set the Specific Page
in your project option (under the Web tab) to the path you want to use (let's say you want to call your home
path, then just fill up the field with home
).
ASP.net MVC - Navigation and highlighting the current link
Check out this blog post
It shows how to create an HTML Extension that you call instead of the usual Html.ActionLink
The extension then appends class="selected"
to the list item that is currently active.
You can then put whatever formatting/highlighting you want in your CSS
EDIT
Just adding some code to rather than just a link.
public static class HtmlHelpers
{
public static MvcHtmlString MenuLink(this HtmlHelper htmlHelper,
string linkText,
string actionName,
string controllerName
)
{
string currentAction = htmlHelper.ViewContext.RouteData.GetRequiredString("action");
string currentController = htmlHelper.ViewContext.RouteData.GetRequiredString("controller");
if (actionName == currentAction && controllerName == currentController)
{
return htmlHelper.ActionLink(linkText, actionName, controllerName, null, new { @class = "selected" });
}
return htmlHelper.ActionLink(linkText, actionName, controllerName);
}
}
Now you need to define your selected
class in your CSS and then in your views add a using
statement at the top.
@using ProjectNamespace.HtmlHelpers
And use the MenuLink
instead of ActionLink
@Html.MenuLink("Your Menu Item", "Action", "Controller")
How to get current page URL in MVC 3
You could use the Request.RawUrl
, Request.Url.OriginalString
, Request.Url.ToString()
or Request.Url.AbsoluteUri
.
How to keep the active menu item highlighted when visiting pages in ASP.Net MVC 5 application?
Everytime you click on a link, it does a page redirect, and it doesn't matter what changes you've made with jQuery, it always fetch this partial view, and in this partial view, you have active class on the first link, that is why it always highlights the first link.
What you have to do is write an HtmlHelper that will add the class "active" to current link on each page load. Create a folder called Helpers under your project and add a custom HtmlHelper class.
using System;
using System.Web.Mvc;
public static class ActiveMenuHelper
{
public static MvcHtmlString MenuLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, string areaName)
{
var currentAction = htmlHelper.ViewContext.RouteData.GetRequiredString("action");
var currentController = htmlHelper.ViewContext.RouteData.GetRequiredString("controller");
var currentArea = htmlHelper.ViewContext.RouteData.DataTokens["area"];
var builder = new TagBuilder("li")
{
//InnerHtml = htmlHelper.ActionLink(linkText, actionName, controllerName).ToHtmlString()
InnerHtml = "<a href=\"" + new UrlHelper(htmlHelper.ViewContext.RequestContext).Action(actionName, controllerName, new { area = areaName }).ToString() + "\">" + linkText + "</a>"
};
if (String.Equals(controllerName, currentController, StringComparison.CurrentCultureIgnoreCase) && String.Equals(actionName, currentAction, StringComparison.CurrentCultureIgnoreCase))
builder.AddCssClass("active");
return new MvcHtmlString(builder.ToString());
}
}
and then in your partial view:
@using YourProjectName.Helpers
<ul>
@Html.MenuLink("Resume", "Index", "Resume", "" )
@Html.MenuLink("Cover Letter", "Index", "CoverLetter", "" )
</ul>
You might not need the area, that is why you can leave it blank, i put it in here just in case.
How masterpage get the current page from masterpage by itself
When you click the link in the navigation bar (in the master page) that should be triggering a controller action that will decide which view content page will be shown. In that action method you can (as Lazarus suggests) add some data identifying the current page to ViewData that will be picked up by the master page to modify the navigation bar.
Related Topics
How to Place an Image Over Another
Is There an Advantage in Grouping CSS Media Queries Together
How to Use SCSS/Sass to Increase Animation-Delay for Concurrent Divs
Css3 Scale Transform on Parent Div But Keeping Constant Size in Some of the Associated Divs
No Border-Spacing Before First Item and After Last Item
Css: How to Set Container Size Equal to Background Image Size
How to Emulate CSS Scroll Snap Points in Chrome
Styling Scrollbar for Google Chrome Browser
Margin-Bottom for <A> Link Elements
CSS !Important Does Not Override Styles from External Stylesheets
How to Reduce Font Weight in Firefox on MAC with CSS
How to Feature-Detect CSS Filters
Does Z-Index Specify the Stack Level of a Non-Positioned Flex Item
Angular2: How to Manually Add CSS Files by Condition to Index.Html