当前位置: 首页 > 编程语言 > C#

ASP.net MVC – 导航并突出显示“当前”链接分享

时间:2023-04-10 21:08:57 C#

C#学习教程:ASP.netMVC-导航并突出显示“当前”链接如果我在该页面上,我想将代码放在这里以突出显示当前链接。如果我添加其他链接,例如:如果我在Products控制器中使用任何操作,我希望Products链接处于活动状态(使用.active之类的css类)。如果我在HomeControllerAbout操作上,那么About链接应该是活动的。如果我在HomeController的Index操作上,Home链接应该处于活动状态。在MVC中执行此操作的最佳方法是什么?查看这篇博文,它展示了如何创建一个您调用的HTML扩展而不是通常的Html.ActionLink扩展,然后将class="selected"附加到当前活动的列表项。然后,您可以在CSS编辑中放置您想要的任何格式/突出显示,只需添加一些代码而不仅仅是一个链接。publicstaticclassHtmlHelpers{publicstaticMvcHtmlStringMenuLink(thisHtmlHelperhtmlHelper,stringlinkText,stringactionName,stringcontrollerName){stringcurrentAction=htmlHelper.ViewContext.RouteData.GetRequiredString("action");stringcurrentRouteController=htmlHelper.StringViewContext("controller");if(actionName==currentAction&&controllerName==currentController){returnhtmlHelper.ActionLink(linkText,actionName,controllerName,null,new{@class="selected"});}返回htmlHelper.ActionLink(linkText,actionName,controllerName);现在,您需要在CSS中定义选定的类,然后在视图的顶部添加一个using语句。@usingProjectNamespace.HtmlHelpers并使用MenuLink而不是ActionLink@Html.MenuLink("YourMenuItem","Action","Controller")您可以使用“data-”属性识别容器,然后使用更改CSS类jQueryforthelinktodothislikethis:@Html.ActionLink("About","About","Home")@Html.ActionLink("Contact","Contact","Home")以下是如何做到这一点MVC帮助程序实现的方法:@helperNavigationLink(stringlinkText,stringactionName,stringcontrollerName){if(ViewContext.RouteData.GetRequiredString("action").Equals(actionName,StringComparison.OrdinalIgnoreCase)&&ViewContext.RouteData.GetRequiredString(“控制器”).Equals(controllerName,StringComparison.OrdinalIgnoreCase)){@linkText}else{@Html.ActionLink(linkText,actionName,controllerName);然后你可以使用类似的东西:@NavigationLink("Home","index","home")@NavigationLink("AboutUs","about","home")关于MVC助手的一篇好文章可以在这里找到:http://weblogs.asp.net/scottgu/archive/2011/05/12/asp-net-mvc-3-and-the-helper-syntax-within-razor.aspx我用htmlhelper解决了这个问题方法:公共统计icclassHtmlHelpers{publicstaticMvcHtmlStringMenuLink(thisHtmlHelperhtmlHelper,stringlinkText,stringactionName,stringcontrollerName){stringcurrentAction=htmlHelper.ViewContext.RouteData.GetRequiredString("action");stringcurrentController=htmlHelper.ViewContext.Route("controller");如果(actionName.Equals(currentAction,StringComparison.InvariantCultureIgnoreCase)&&controllerName.Equals(currentController,StringComparison.InvariantCultureIgnoreCase)){returnhtmlHelper.ActionLink(linkText,actionName,controllerName,null,new{@class="active"});}返回htmlHelper.ActionLink(linkText,actionName,controllerName);}}和透视@Html.MenuLink"Linktext","action","controller")你可能想看看我的系列MVC导航控件,其中有自动突出显示当前链接的功能:http://mvcquicknav.apphb.com/感谢@codingbadger提供的解决方案,我不得不在多个操作上突出显示我的导航链接,因此我决定添加一些包含控制器操作的权利,如果还访问了这些组合中的任何一个,它将突出显示链接。而且,在我的例子中,高亮类将应用于元素。我把我的代码放在这里,希望它能帮助将来的人:首先制作一个Helper类和HTMLHelper方法publicstaticstringIsActive(thisHtmlHelperhtml,stringcontrol,stringaction){varrouteData=html.ViewContext.路由数据;varrouteAction=(string)routeData.Values["action"];varrouteControl=(string)routeData.Values["controller"];//两者都必须匹配varreturnActive=control==routeControl&&action==routeAction;返回返回活动?“积极的”:””;在View或Layout部分,使用适当的Conntroller和Action调用Helper方法。@usingYourNamespace.HtmlHelpermethodName这样会在class属性中添加"active"字符串,会出现如上图有用,需要多了解C#学习教程,希望大家多多关注—@Html.ActionLink("Home","Index","Home")@Html.ActionLink("About","About","Home")@Html.ActionLink("Contact","Contact","Home")@Html.ActionLink("产品","索引","产品")("Suppliers","Index","Suppliers")@Html.Partial("_LoginPartial")本文来自网络收藏不代表立场,如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: