Highlight کردن لینک صفحه جاری در ASP.NET MVC
نویسنده: علیرضا اسمرام
تاریخ: ۱۳۹۱/۰۶/۱۹ ۲۲:۵۷
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
public static MvcHtmlString MenuLink(this HtmlHelper helper, string text, string action, string controller)
{
var routeData = helper.ViewContext.RouteData.Values;
var currentController = routeData["controller"];
var currentAction = routeData["action"];
if(String.Equals(action, currentAction as string,
StringComparison.OrdinalIgnoreCase)
&&
String.Equals(controller, currentController as string,
StringComparison.OrdinalIgnoreCase))
{
return helper.ActionLink(
text,action, controller, null,
new { @class="currentMenuItem"}
);
}
return helper.ActionLink(text, action, controller);
}
<li>@Html.MenuLink("Contact", "Contact", "Home")</li>
ul#menu li a {
background: none;
color: #999;
padding: 5px;
border-radius: 15px;
text-decoration: none;
}
ul#menu li a.currentMenuItem {
background-color: black;
color: white;
}
public static MvcHtmlString MenuLinkBootstrap(this HtmlHelper helper, string text, string action, string controller)
{
var routeData = helper.ViewContext.RouteData.Values;
var currentController = routeData["controller"];
var currentAction = routeData["action"];
if (String.Equals(action, currentAction as string, StringComparison.OrdinalIgnoreCase) && String.Equals(controller, currentController as string, StringComparison.OrdinalIgnoreCase))
{
return new MvcHtmlString("<li class=\"active\">" + helper.ActionLink(text, action, controller) + "</li>");
}
return new MvcHtmlString("<li>" + helper.ActionLink(text, action, controller) + "</li>");
}
//--------------انتخاب خودکار لینکهای بالای صفحه به ازای صفحه جاری
$(document).ready(function () {
$("#headermenu a").each(function () {
var $a = $(this);
var href = $a.attr("href");
if (href && (location.pathname.toLowerCase() == href.toLowerCase())) {
//صفحه جاری را یافتیم
$a.css({
"color": "Yellow",
"border-bottom": "1px solid"
});
}
});
});
if (typeof String.prototype.startsWith != 'function') {
// see below for better implementation!
String.prototype.startsWith = function (str) {
return this.indexOf(str) == 0;
};
}
$(document).ready(function () {
$("#headermenu a").each(function () {
var $a = $(this);
var href = $a.attr("href");
var path = $.trim(location.pathname.toLowerCase());
var menuHref = $.trim(href.toLowerCase());
if (href && (path == menuHref || (menuHref != '/' && path.startsWith(menuHref)))) {
//صفحه جاری را یافتیم
$a.css({
"color": "Yellow",
"border-bottom": "2px solid"
});
}
});
});$(function(){
var loc = location.pathname.toLowerCase();
$('#headermenu a').filter(function(){
var href = this.getAttribute('href');
return href && href.toLowerCase() == loc;
}).addClass('highlight');
});$('#headermenu a[href="' + location.pathname + '"]').addClass('highlight');$(document).ready(function () {
$('ul.nav.navbar-nav, ul.list-group, ul.nav.nav-tabs').find('a[href="' + location.pathname + '"]')
.closest('li')
.addClass('active');
});