ASP.net MVC - 导航并突出显示“当前”内容关联
当您创建一个新的 MVC 项目时,它会创建一个具有以下标记的 Site.master:
<div id="menucontainer">
<ul id="menu">
<li><%: Html.ActionLink("Home", "Index", "Home")%></li>
<li><%: Html.ActionLink("About", "About", "Home")%></li>
</ul>
</div>
我想在此处放置代码,如果我在该页面上,它将突出显示当前链接。
如果我添加另一个链接,例如:
<li><%: Html.ActionLink("Products", "Index", "Products")%></li>
如果我在产品控制器中进行任何操作,我希望产品链接处于活动状态(使用 .active 之类的 css 类)。
如果我处于 HomeController 关于操作,则“关于”链接应该处于活动状态。如果我正在执行 HomeController 的 Index 操作,则 Home 链接应该处于活动状态。
在 MVC 中执行此操作的最佳方法是什么?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
查看 这篇博文
它展示了如何创建一个您调用的 HTML 扩展,而不是通常的
Html.ActionLink
该扩展然后附加class="selected"< /code> 到当前活动的列表项。
然后,您可以在 CSS 中添加任何您想要的格式/突出显示
编辑,
只需添加一些代码而不仅仅是链接。
现在,您需要在 CSS 中定义
selected
类,然后在视图中在顶部添加using
语句。@using ProjectNamespace.HtmlHelpers
并使用
MenuLink
而不是ActionLink
@Html.MenuLink("Your Menu Item", "Action" ,“控制器”)
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 appendsclass="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.
Now you need to define your
selected
class in your CSS and then in your views add ausing
statement at the top.@using ProjectNamespace.HtmlHelpers
And use the
MenuLink
instead ofActionLink
@Html.MenuLink("Your Menu Item", "Action", "Controller")
您可以通过使用“data-”属性来标识容器,然后使用 jQuery 更改链接的 CSS 类来完成此操作,如下所示:
You could do this by using "data-" attributes to identify the container(s) then using jQuery change CSS class of the link, like the following:
下面是一种将其实现为 MVC 帮助程序的方法:
然后可以类似于以下方式使用它:
可以在此处找到有关 MVC 帮助程序的好文章: http://weblogs.asp.net/scottgu/archive/ 2011/05/12/asp-net-mvc-3-and-the-helper-syntax-within-razor.aspx
Here is a way to implement this as an MVC helper:
It can then be used similar to the following:
A good article on MVC helpers can be found here: http://weblogs.asp.net/scottgu/archive/2011/05/12/asp-net-mvc-3-and-the-helper-syntax-within-razor.aspx
我将这种方法与 htmlhelper 一起使用来解决问题:
以及视图
I Used this approach with a htmlhelper for the problem:
and for the view
您可能想查看我的 MVC 导航控件系列,其中包括自动突出显示当前链接的功能:
http:// mvcquicknav.apphb.com/
You might like to check out my series of MVC navigation controls, which includes the ability to automatically highlight the current link:
http://mvcquicknav.apphb.com/
感谢@codingbadger 提供的解决方案。
我必须在多个操作上突出显示我的导航链接,因此我决定添加更多包含控制器操作对的参数,如果也访问了这些组合中的任何一个,它将突出显示链接。而且,就我而言,突出显示类将应用于
元素。
我将代码放在这里希望它能帮助将来的人:
这是辅助方法:
这里是如何使用它的示例:
Thanks to @codingbadger for the solution.
I had to get my nav-links highlighted on multiple actions so I decided to add a few more parameters that contain the controller-action pairs and it'll highlight the link if either of those combinations is accessed too. And, in my case, the highlighting class was to be applied to a
<li>
element.I'm putting my code here hoping it will help someone in the future:
Here's the helper method:
And, here's an example of how to use it:
首先创建一个帮助程序类和 HTML 帮助程序方法
,然后在视图或布局部分中只需使用适当的控制器和操作调用帮助程序方法。
这将在类属性中添加“active”字符串,它将显示为
First Make a Helper Class and HTML Helper method
And in View or Layour Section Just Call the Helper Method with appropriate Conntroller and Action.
this will add "active" string in class attribute and it will show like