微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

asp.net-mvc – 在ASP.NET MVC中使用控制器和用户控件设置活动选项卡的简单方法?

如何使用用户界面中突出显示的“当前”标签创建标签式导航?

解决方法

在MVC之前,我查看了文件路径,并找出哪个标签是currrent.现在这很简单,您可以根据当前控制器分配当前选项卡.

一探究竟 …

大多数工作发生在用户控件中.

public partial class AdminNavigation : ViewUserControl
{
    /// <summary>
    /// This hold a collection of controllers and their respective "tabs." Each Tab should have at least one controller in the collection.
    /// </summary>
    private readonly IDictionary<Type,string> dict = new Dictionary<Type,string>();

    public AdminNavigation()
    {
        dict.Add(typeof(BrandController),"catalog");
        dict.Add(typeof(CatalogController),"catalog");
        dict.Add(typeof(GroupController),"catalog");
        dict.Add(typeof(ItemController),"catalog");
        dict.Add(typeof(ConfigurationController),"configuration");
        dict.Add(typeof(CustomerController),"customer");
        dict.Add(typeof(DashboardController),"dashboard");
        dict.Add(typeof(OrderController),"order");
        dict.Add(typeof(WebsiteController),"website");
    }

    protected string SetClass(string linktocheck)
    {
        Type controller = ViewContext.Controller.GetType();
        // We need to determine if the linktocheck is equal to the current controller using dict as a Map
        string dictValue;
        dict.TryGetValue(controller,out dictValue);

        if (dictValue == linktocheck)
        {
            return "current";
        }
        return "";
    }
}

然后在您的.ascx部分usercontol调用SetClass方法来检查与dict相关的链接.像这样:

<li class="<%= SetClass("customer") %>"><%= Html.ActionLink<CustomerController>(c=>c.Index(),"Customers",new{@class="nav_customers"}) %></li>

现在你需要的是CSS来突出显示你当前的标签.有一些不同的方法来做到这一点,但是你可以在这里开始一些想法:http://webdeveloper.econsultant.com/css-menus-navigation-tabs/
哦,别忘了把用户控件放在你的页面上(或MasterPage)…

<% Html.RenderPartial("AdminNavigation"); %>

原文地址:https://www.jb51.cc/aspnet/250453.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐