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博客园博客,原文链接:http://www.cnblogs.com/jinzhao/archive/2012/07/23/2605401.html,如需转载请自行联系原作者

相关文章
|
前端开发 Java API
Spring MVC相关异常类
Spring MVC相关异常类
234 0
|
前端开发 IDE Java
Spring MVC 中因导入错误的 Model 类报错问题解析
在 Spring MVC 或 Spring Boot 开发中,若导入错误的 `Model` 类(如 `ch.qos.logback.core.model.Model`),会导致无法解析 `addAttribute` 方法的错误。正确类应为 `org.springframework.ui.Model`。此问题通常因 IDE 自动导入错误类引起。解决方法包括:删除错误导入、添加正确包路径、验证依赖及清理缓存。确保代码中正确使用 Spring 提供的 `Model` 接口以实现前后端数据传递。
621 0
|
前端开发 JavaScript
MVC中简单数据模型(M): Model类
MVC中简单数据模型(M): Model类
|
XML 前端开发 Java
SpringMVC中<mvc:annotation-driven/>标签原理与实践详解
SpringMVC中<mvc:annotation-driven/>标签原理与实践详解
551 0
ssm(Spring+Spring mvc+mybatis)Service层实现类——DeptServiceImpl
ssm(Spring+Spring mvc+mybatis)Service层实现类——DeptServiceImpl
ssm(Spring+Spring mvc+mybatis)Dao层实现类——DeptDaoImpl
ssm(Spring+Spring mvc+mybatis)Dao层实现类——DeptDaoImpl
|
缓存 前端开发 Java
【Spring底层原理高级进阶】轻松掌握 Spring MVC 的拦截器机制:深入理解 HandlerInterceptor 接口和其实现类的用法
【Spring底层原理高级进阶】轻松掌握 Spring MVC 的拦截器机制:深入理解 HandlerInterceptor 接口和其实现类的用法
|
前端开发 Java 索引
Spring MVC Controller 方法参数 Map 的实现类是什么?
问题 题主问题描述如下: 在SpringBoot中,Controller的参数中有Map接口类型的,请问他的实现类是什么? 突发奇想,在SpringBoot中,Controller的参数中有Map接口类型的
756 0
Spring MVC Controller 方法参数 Map 的实现类是什么?
|
存储 前端开发 Java
Spring Boot中Spring MVC的表单标签库与数据绑定讲解与实战(附源码 超详细必看)
Spring Boot中Spring MVC的表单标签库与数据绑定讲解与实战(附源码 超详细必看)
440 0
|
XML JSON 前端开发
<mvc:annotation-driven />标签详解
你好看官,里面请!今天笔者讲的是在Spring MVC配置静态资源。不懂或者觉得我写的有问题可以在评论区留言,我看到会及时回复。 注意:本文仅用于学习参考,不可用于商业用途,如需转载请跟我联系。
559 2