使用c#MVC3 Html.ActionLink渲染引导程序下拉菜单

时间:2013-11-12 19:10:39

标签: c# asp.net-mvc twitter-bootstrap razor asp.net-mvc-routing

因此我遇到了MVC ActionLink和bootstrap下拉列表的问题。我成功了简单的菜单扩展,我传递了像字符串和一个bool这样的参数。但是现在我正在尝试创建自己的扩展,它可以生成Bootstrap Dropdown并将选定的css类添加到下拉列表的父级 - “ONEofTHEdropdownITEMSselected” - 当选择下拉列表中的某个项目时(选择下拉列表时)它路由到不同控制器的项目可以是少数或多个控制器):

<a href="#" class="dropdown-toggle ONEofTHEdropdownITEMSselected" data-toggle="dropdown">Dropdown <b class="caret"></b></a>

<li class="dropdown">
    <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown <b class="caret"></b></a>
    <ul class="dropdown-menu">
        <li><a href="~/area/controller1">Action1</a></li>
        <li><a href="~/area/controller2">Action2</a></li>
    </ul>
</li>

下面是我的UI / MenuExtensions.cs我想要实现的 - 传递两个可以生成引导下拉列表的参数,我可以在该下拉列表中手动插入新的菜单项。

public static class MenuExtensions
    {
        public static MvcHtmlString MenuItem(
            this HtmlHelper htmlHelper,
            string text,
            string action,
            string controller,
            string cssClass = "item",
            bool isController = false
        )
        {
            var li = new TagBuilder("li");
            var routeData = htmlHelper.ViewContext.RouteData;
            var currentAction = routeData.GetRequiredString("action");
            var currentController = routeData.GetRequiredString("controller");

            if ((string.Equals(currentAction, action, StringComparison.OrdinalIgnoreCase) || isController) &&
                 string.Equals(currentController, controller, StringComparison.OrdinalIgnoreCase))
                li.AddCssClass("am-selected");

            li.InnerHtml = htmlHelper.ActionLink(text, action, controller, new { Area = "" }, new { @class = cssClass }).ToHtmlString();
            return MvcHtmlString.Create(li.ToString());
        }

      public static MvcHtmlString SelectMenu(
            this HtmlHelper htmlHelper,
            string cssClass,
            SelectMenuItem[] menuItems
        )
        {
            TagBuilder list = new TagBuilder("li")
            {
                InnerHtml = ""
            };
            string currentAction = htmlHelper.ViewContext.RouteData.GetRequiredString("action");
            string currentController = htmlHelper.ViewContext.RouteData.GetRequiredString("controller");

            foreach (SelectMenuItem menuItem in menuItems)
            {
                TagBuilder li = new TagBuilder("li")
                {
                    InnerHtml = htmlHelper.ActionLink(menuItem.Text, menuItem.Action, menuItem.Controller, null, new { }).ToHtmlString()
                };
                ul.InnerHtml += li.ToString();
            }

            return MvcHtmlString.Create(list.ToString());
        }
    }

这是外部课程

 public class SelectMenuItem
    {
        public string Text { get; set; }
        public string Action { get; set; }
        public string Controller { get; set; }
        public bool IsVisible { get; set; }

        public SelectMenuItem()
        {
            IsVisible = true;
        }
    }

之后我的html看起来像这样。

 @Html.SelectMenu("dropdown", new []{
                            new SelectMenuItem{ Text = "ViewOne", Controller = "Controller1", Action = "index", IsVisible = SystemUser.Current().IsAdmin},
                            new SelectMenuItem{ Text = "ViewTwo", Controller = "Controller2", Action = "index"}
                        });

问题是SelectMenu仅渲染:

<li></li>

1 个答案:

答案 0 :(得分:4)

无需重新发明轮子。使用TwitterBootstrapMVC,可以使用以下语法实现所需的输出:

@using (var dd = Html.Bootstrap().Begin(new DropDown("Dropdown").SetLinksActiveByControllerAndAction()))
{
    @dd.ActionLink("Action1", "index", "controller1")
    @dd.ActionLink("Action2", "index", "controller2")
}

请注意扩展方法SetLinksActiveByControllerAndAction()。这就是基于当前控制器/操作使链接处于活动状态的原因。


免责声明:我是TwitterBootstrapMVC的作者。

如果使用Bootstrap 3,您需要购买许可证。对于Bootstrap 2,它是免费的。