Skip to content

Instantly share code, notes, and snippets.

@mburumaxwell
Created June 10, 2013 14:28
Show Gist options
  • Save mburumaxwell/5749109 to your computer and use it in GitHub Desktop.
Save mburumaxwell/5749109 to your computer and use it in GitHub Desktop.
Setting the selected menu item in ASP.NET MVC
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>@ViewBag.Title - My ASP.NET MVC Application</title>
<link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
<meta name="viewport" content="width=device-width" />
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
</head>
<body>
<header>
<div class="content-wrapper">
<div class="float-left">
<p class="site-title">@Html.ActionLink("your logo here", "Index", "Home")</p>
</div>
<div class="float-right">
<section id="login">
@Html.Partial("_LoginPartial")
</section>
<nav>
<ul id="menu">
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
<li>@Html.ActionLink("Contact", "Contact", "Home")</li>
</ul>
</nav>
</div>
</div>
</header>
<div id="body">
@RenderSection("featured", required: false)
<section class="content-wrapper main-content clear-fix">
@RenderBody()
</section>
</div>
<footer>
<div class="content-wrapper">
<div class="float-left">
<p>&copy; @DateTime.Now.Year - My ASP.NET MVC Application</p>
</div>
</div>
</footer>
@Scripts.Render("~/bundles/jquery")
@RenderSection("scripts", required: false)
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>@ViewBag.Title - My ASP.NET MVC Application</title>
<link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
<meta name="viewport" content="width=device-width" />
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
</head>
<body>
<header>
<div class="content-wrapper">
<div class="float-left">
<p class="site-title">@Html.ActionLink("your logo here", "Index", "Home")</p>
</div>
<div class="float-right">
<section id="login">
@Html.Partial("_LoginPartial")
</section>
<nav>
<ul id="menu">
@Html.ActionMenuItem("Home", "Index", "Home", new { area = "" }, null)
@Html.ActionMenuItem("About", "About", "Home", new { area = "" }, null)
@Html.ActionMenuItem("Contact", "Contact", "Home", new { area = "" }, null)
@Html.ActionMenuItem("Admin", "Index", "AdminHome", new { area = "admin" }, null)
</ul>
</nav>
</div>
</div>
</header>
<div id="body">
@RenderSection("featured", required: false)
<section class="content-wrapper main-content clear-fix">
@RenderBody()
</section>
</div>
<footer>
<div class="content-wrapper">
<div class="float-left">
<p>&copy; @DateTime.Now.Year - My ASP.NET MVC Application</p>
</div>
</div>
</footer>
@Scripts.Render("~/bundles/jquery")
@RenderSection("scripts", required: false)
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Routing;
namespace MvcApplication1 {
public static class HtmlExtensions {
public static MvcHtmlString ActionMenuItem(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, object routeValues, object htmlAttributes) {
var tag = new TagBuilder("li");
var routeData = htmlHelper.ViewContext.RouteData;
var controller = routeData.GetRequiredString("controller");
var action = routeData.GetRequiredString("action");
var area = routeData.DataTokens["area"] as string;
var routesGiven = routeValues == null ? null : new RouteValueDictionary(routeValues);
object an = null;
if (routesGiven != null) routesGiven.TryGetValue("area", out an);
var areaName = an as string;
bool active = ((string.IsNullOrEmpty(area) && string.IsNullOrEmpty(areaName)) || (area.Equals(areaName, StringComparison.OrdinalIgnoreCase)));
active &= ((string.IsNullOrEmpty(controller) && string.IsNullOrEmpty(controllerName)) || (controller.Equals(controllerName, StringComparison.OrdinalIgnoreCase)));
active &= ((string.IsNullOrEmpty(action) && string.IsNullOrEmpty(actionName)) || (action.Equals(actionName, StringComparison.OrdinalIgnoreCase)));
if (active) tag.AddCssClass("active");
tag.InnerHtml = htmlHelper.ActionLink(linkText, actionName, controllerName, routeValues, htmlAttributes).ToString();
return MvcHtmlString.Create(tag.ToString());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment