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"); %>

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....