MVC标签链接切换帮助类: TabLink
写Tab时为了保证慢加载下tab输出不乱必须在服务端写,然后就是出现了很多难看的if else,今天花了点时间写了个帮助类,跟ActionLink的使用一样。
示例代码:
<ul> <li>@Html.TabLink( "新闻" , "s" , new { t = "n" }, new { onclick = "return channelSwitch('n');" }, "tab_selected" )</li> <li>@Html.TabLink( "全部" , "s" , new { t = "" }, new { onclick = "return channelSwitch(0);" }, "tab_selected" )</li> <li>@Html.TabLink( "博客" , "s" , new { t = "b" }, new { onclick = "return channelSwitch('b');" }, "tab_selected" )</li> <li>@Html.TabLink( "知识库" , "s" , new { t = "k" }, new { onclick = "return channelSwitch('k');" }, "tab_selected" )</li> <li>@Html.TabLink( "博问" , "s" , new { t = "q" }, new { onclick = "return channelSwitch('q');" }, "tab_selected" )</li> </ul> |
TabLink代码:
#region TabLinks public static MvcHtmlString TabLink( this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName = null , string currentClass = "current" ) { return TabLink(htmlHelper, linkText, actionName, controllerName, new RouteValueDictionary(), new RouteValueDictionary(), currentClass); } public static MvcHtmlString TabLink( this HtmlHelper htmlHelper, string linkText, string actionName, object routeValues, string currentClass = "current" ) { return TabLink(htmlHelper, linkText, actionName, null /* controllerName */ , new RouteValueDictionary(routeValues), new RouteValueDictionary(), currentClass); } public static MvcHtmlString TabLink( this HtmlHelper htmlHelper, string linkText, string actionName, object routeValues, object htmlAttributes, string currentClass = "current" ) { return TabLink(htmlHelper, linkText, actionName, null /* controllerName */ , new RouteValueDictionary(routeValues), HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes), currentClass); } public static MvcHtmlString TabLink( this HtmlHelper htmlHelper, string linkText, string actionName, RouteValueDictionary routeValues, string currentClass = "current" ) { return TabLink(htmlHelper, linkText, actionName, null /* controllerName */ , routeValues, new RouteValueDictionary(), currentClass); } public static MvcHtmlString TabLink( this HtmlHelper htmlHelper, string linkText, string actionName, RouteValueDictionary routeValues, IDictionary< string , object > htmlAttributes, string currentClass = "current" ) { return TabLink(htmlHelper, linkText, actionName, null /* controllerName */ , routeValues, htmlAttributes, currentClass); } public static MvcHtmlString TabLink( this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, object routeValues, object htmlAttributes, string currentClass = "current" ) { return TabLink(htmlHelper, linkText, actionName, controllerName, new RouteValueDictionary(routeValues), HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes), currentClass); } public static MvcHtmlString TabLink( this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, RouteValueDictionary routeValues, IDictionary< string , object > htmlAttributes, string currentClass = "current" ) { if (String.IsNullOrEmpty(linkText)) { throw new ArgumentException( "Value cannot be null or empty." , "linkText" ); } if (IsCurrent(htmlHelper, actionName, controllerName, routeValues)) { htmlAttributes = htmlAttributes ?? new Dictionary< string , object >(); string classValue = currentClass; if (htmlAttributes.ContainsKey( "class" )) { classValue = htmlAttributes[ "class" ] + classValue; } htmlAttributes[ "class" ] = classValue; } return MvcHtmlString.Create(HtmlHelper.GenerateLink(htmlHelper.ViewContext.RequestContext, htmlHelper.RouteCollection, linkText, null /* routeName */ , actionName, controllerName, routeValues, htmlAttributes)); } public static MvcHtmlString TabLink( this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, string protocol, string hostName, string fragment, object routeValues, object htmlAttributes, string currentClass = "current" ) { return TabLink(htmlHelper, linkText, actionName, controllerName, protocol, hostName, fragment, new RouteValueDictionary(routeValues), HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes), currentClass); } public static MvcHtmlString TabLink( this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, string protocol, string hostName, string fragment, RouteValueDictionary routeValues, IDictionary< string , object > htmlAttributes, string currentClass = "current" ) { if (String.IsNullOrEmpty(linkText)) { throw new ArgumentException( "Value cannot be null or empty." , "linkText" ); } if (IsCurrent(htmlHelper, actionName, controllerName, routeValues)) { htmlAttributes = htmlAttributes ?? new Dictionary< string , object >(); string classValue = currentClass; if (htmlAttributes.ContainsKey( "class" )) { classValue = htmlAttributes[ "class" ] + classValue; } htmlAttributes[ "class" ] = classValue; } return MvcHtmlString.Create(HtmlHelper.GenerateLink(htmlHelper.ViewContext.RequestContext, htmlHelper.RouteCollection, linkText, null /* routeName */ , actionName, controllerName, protocol, hostName, fragment, routeValues, htmlAttributes)); } private static bool IsCurrent( this HtmlHelper htmlHelper, string actionName, string controllerName, RouteValueDictionary routeValues) { var isCurrent = htmlHelper.RouteValue( "action" ).ToLower() == actionName.ToLower(); if (! string .IsNullOrEmpty(controllerName)) { isCurrent &= htmlHelper.RouteValue( "controller" ).ToLower() == controllerName.ToLower(); } if (routeValues == null ) { return isCurrent; } var rvEnum = routeValues.GetEnumerator(); while (isCurrent && rvEnum.MoveNext()) { isCurrent = rvEnum.Current.Value.ToString().ToLower() == htmlHelper.RouteValue(rvEnum.Current.Key).ToLower(); } return isCurrent; } private static string RouteValue( this HtmlHelper htmlHelper, string key) { var v = htmlHelper.ViewContext.Controller.ValueProvider.GetValue(key); if (v== null ) return string .Empty; return v.ConvertTo( typeof ( string )) as string ; } #endregion |
作者:today4king
出处:https://www.cnblogs.com/jinzhao/archive/2012/07/23/2605401.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架