加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 在ASP.NET MVC中使用控制器和用户控件设置活动选

发布时间:2020-12-15 23:30:04 所属栏目:asp.Net 来源:网络整理
导读:如何使用用户界面中突出显示的“当前”标签创建标签式导航? 解决方法 在MVC之前,我查看了文件路径,并找出哪个标签是currrent.现在这很简单,您可以根据当前控制器分配当前选项卡. 一探究竟 … 大多数工作发生在用户控件中. public partial class AdminNaviga
如何使用用户界面中突出显示的“当前”标签创建标签式导航?

解决方法

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读