static string GetTagLI(HtmlHelper htmlHelper, VM.SiteMenu menu, string area, string controller, string action, int menuLevel) { TagBuilder LiTag = new TagBuilder("li"); string activeCSS = string.Empty; string dropdownCSS = "dropdown"; string caret = "caret"; string strUrl = UrlHelper.GenerateUrl(null, menu.ActionName, menu.Controller, null, null, null, new RouteValueDictionary(new { area = menu.Area }), htmlHelper.RouteCollection, htmlHelper.ViewContext.RequestContext, true); StringBuilder LiInnerHtml = new StringBuilder(200); if (menu.Area.Equals(area, StringComparison.InvariantCultureIgnoreCase) && menu.Controller.Equals(controller, StringComparison.InvariantCultureIgnoreCase) && menu.ActionName.Equals(action, StringComparison.InvariantCultureIgnoreCase)) { activeCSS = "active"; } if (menuLevel >= 1) { dropdownCSS = "dropdown-submenu"; caret = "submenuCaret"; } menuLevel++; //NamingItems NamingItems = new NamingItems(companyId); //if(System.Web.HttpRuntime.Cache["Menu"] == null) //{ // NamingItems=new NamingItems(companyId); // System.Web.HttpRuntime.Cache["Menu"] = NamingItems; //} //else //{ // NamingItems=(NamingItems)System.Web.HttpRuntime.Cache["Menu"]; //} if (menu.ChildrenMenu != null && menu.ChildrenMenu.Count > 0) { //<li class="@activeCSS @dropdownCSS" ><a href="@Url.Action(Model.ActionName, Model.Controller, new { area = Model.Area })" class="dropdown-toggle" data-toggle="dropdown" >@ResourceHelper.GetResourceWithCustom(typeof(CommonResource),Model.ResourceKey,companyCode) // <b class="@caret"></b></a> // @Html.Partial("_NavBarMenuNodes", Model.ChildrenMenu) //</li> TagBuilder aTag = new TagBuilder("a"); TagBuilder bTag = new TagBuilder("b"); StringBuilder aInnerHtml = new StringBuilder(200); LiTag.AddCssClass(string.Format("{0} {1}", activeCSS, dropdownCSS)); aTag.MergeAttribute("href", strUrl); aTag.AddCssClass("dropdown-toggle"); aTag.MergeAttribute("data-toggle", "dropdown"); bTag.AddCssClass(caret); //aInnerHtml.Append(ResourceHelper.GetResourceObject(typeof(CommonResource), menu.ResourceKey)); aInnerHtml.Append(menu.ResourceKey); //aInnerHtml.Append(NamingItems.GetNamingFormatValueFromDefaultDisplayType(menu.ResourceKey)); aInnerHtml.Append(bTag.ToString(TagRenderMode.Normal)); aTag.InnerHtml = aInnerHtml.ToString(); LiInnerHtml.Append(aTag.ToString(TagRenderMode.Normal)); LiInnerHtml.Append(GetTagUL(htmlHelper, menu.ChildrenMenu, area, controller, action, menuLevel)); } else { //<li class="@activeCSS" ><a href="@Url.Action(Model.ActionName, Model.Controller, new { area = Model.Area })" >@ResourceHelper.GetResourceWithCustom(typeof(CommonResource),Model.ResourceKey,companyCode)</a></li> TagBuilder aTag = new TagBuilder("a"); LiTag.AddCssClass(string.Format("{0}", activeCSS)); aTag.MergeAttribute("href", strUrl); aTag.InnerHtml = menu.ResourceKey; // ResourceHelper.GetResourceObject(typeof(CommonResource), menu.ResourceKey); //aTag.InnerHtml = NamingItems.GetNamingFormatValueFromDefaultDisplayType(menu.ResourceKey); LiInnerHtml.Append(aTag.ToString(TagRenderMode.Normal)); } LiTag.InnerHtml = LiInnerHtml.ToString(); return(LiTag.ToString(TagRenderMode.Normal)); }
//菜单每次点周后都刷掉了原来展开的层次????? //<li class="active"><a href="#"><i class="fa fa-link"></i> <span>Another Link</span></a></li> static string GetTagLILTE(HtmlHelper htmlHelper, VM.SiteMenu menu, string area, string controller, string action, int menuLevel) { TagBuilder li_Tag = new TagBuilder("li"); string activeCSS = string.Empty; string treeviewCSS = "treeview"; string strUrl = UrlHelper.GenerateUrl(null, menu.ActionName, menu.Controller, null, null, null, new RouteValueDictionary(new { area = menu.Area }), htmlHelper.RouteCollection, htmlHelper.ViewContext.RequestContext, true); StringBuilder li_InnerHtml = new StringBuilder(200); //点击菜单到达的页面,再点击页面里的操作,还是会刷掉已选菜单项,未来判断需加上找到页面里的action是不是在这个菜单下 bool isActive = MultiLeveMenuContainsActiveItem(menu.ChildrenMenu, area, controller, action); if ( (menu.Area.Equals(area, StringComparison.InvariantCultureIgnoreCase) && menu.Controller.Equals(controller, StringComparison.InvariantCultureIgnoreCase) && menu.ActionName.Equals(action, StringComparison.InvariantCultureIgnoreCase)) || isActive ) { activeCSS = "active"; } if (menuLevel >= 1) { //dropdownCSS = "treeview"; //caret = "submenuCaret"; } menuLevel++; if (menu.ChildrenMenu != null && menu.ChildrenMenu.Count > 0) { //<li class="@activeCSS @dropdownCSS" ><a href="@Url.Action(Model.ActionName, Model.Controller, new { area = Model.Area })" class="dropdown-toggle" data-toggle="dropdown" >@ResourceHelper.GetResourceWithCustom(typeof(CommonResource),Model.ResourceKey,companyCode) // <b class="@caret"></b></a> // @Html.Partial("_NavBarMenuNodes", Model.ChildrenMenu) //</li> TagBuilder li_a_Tag = new TagBuilder("a"); TagBuilder li_a_i_Tag = new TagBuilder("i"); TagBuilder li_a_span1_Tag = new TagBuilder("span"); TagBuilder li_a_span2_Tag = new TagBuilder("span"); TagBuilder li_a_span2_i_Tag = new TagBuilder("i"); li_a_i_Tag.AddCssClass(string.Format("{0}", "fa fa-folder")); //class="fa fa-folder"或class="fa fa-link"或class="fa fa-circle-o"== li_a_span1_Tag.InnerHtml = menu.ResourceKey; li_a_span2_Tag.AddCssClass(string.Format("{0}", "pull-right-container")); li_a_span2_i_Tag.AddCssClass(string.Format("{0}", "fa fa-angle-left pull-right")); StringBuilder li_a_span2_InnerHtml = new StringBuilder(200); li_a_span2_InnerHtml.Append(li_a_span2_i_Tag.ToString(TagRenderMode.Normal)); li_a_span2_Tag.InnerHtml = li_a_span2_InnerHtml.ToString(); StringBuilder li_a_InnerHtml = new StringBuilder(200); li_a_InnerHtml.Append(li_a_i_Tag.ToString(TagRenderMode.Normal)); li_a_InnerHtml.Append(li_a_span1_Tag.ToString(TagRenderMode.Normal)); li_a_InnerHtml.Append(li_a_span2_Tag.ToString(TagRenderMode.Normal)); li_Tag.AddCssClass(string.Format("{0} {1}", activeCSS, treeviewCSS)); //li_a_Tag.MergeAttribute("href", strUrl); //li_a_Tag.AddCssClass("dropdown-toggle"); //li_a_Tag.MergeAttribute("data-toggle", "dropdown"); //bTag.AddCssClass(caret); //aInnerHtml.Append(ResourceHelper.GetResourceObject(typeof(CommonResource), menu.ResourceKey)); //li_a_InnerHtml.Append(menu.ResourceKey); //aInnerHtml.Append(NamingItems.GetNamingFormatValueFromDefaultDisplayType(menu.ResourceKey)); //li_a_InnerHtml.Append(bTag.ToString(TagRenderMode.Normal)); li_a_Tag.InnerHtml = li_a_InnerHtml.ToString(); li_InnerHtml.Append(li_a_Tag.ToString(TagRenderMode.Normal)); li_InnerHtml.Append(GetTagULLTE(htmlHelper, menu.ChildrenMenu, area, controller, action, menuLevel)); } else { //<li class="@activeCSS" ><a href="@Url.Action(Model.ActionName, Model.Controller, new { area = Model.Area })" >@ResourceHelper.GetResourceWithCustom(typeof(CommonResource),Model.ResourceKey,companyCode)</a></li> TagBuilder li_a_Tag = new TagBuilder("a"); li_Tag.AddCssClass(string.Format("{0}", activeCSS)); li_a_Tag.MergeAttribute("href", strUrl); //aTag.InnerHtml = menu.ResourceKey; // ResourceHelper.GetResourceObject(typeof(CommonResource), menu.ResourceKey); //aTag.InnerHtml = NamingItems.GetNamingFormatValueFromDefaultDisplayType(menu.ResourceKey); TagBuilder li_a_i_Tag = new TagBuilder("i"); if (menuLevel > 1) { //fa fa-circle-o小图标的样式放到数据表里配置更好 li_a_i_Tag.AddCssClass(string.Format("{0}", "fa fa-circle-o")); //class="fa fa-folder"或class="fa fa-link"或class="fa fa-circle-o"== } else { li_a_i_Tag.AddCssClass(string.Format("{0}", "fa fa-folder")); //class="fa fa-folder"或class="fa fa-link"或class="fa fa-circle-o"== } TagBuilder li_a_span_Tag = new TagBuilder("span"); li_a_span_Tag.InnerHtml = menu.ResourceKey; StringBuilder li_a_InnerHtml = new StringBuilder(200); li_a_InnerHtml.Append(li_a_i_Tag.ToString(TagRenderMode.Normal)); li_a_InnerHtml.Append(li_a_span_Tag.ToString(TagRenderMode.Normal)); li_a_Tag.InnerHtml = li_a_InnerHtml.ToString(); li_InnerHtml.Append(li_a_Tag.ToString(TagRenderMode.Normal)); } li_Tag.InnerHtml = li_InnerHtml.ToString(); return(li_Tag.ToString(TagRenderMode.Normal)); }