zoukankan      html  css  js  c++  java
  • 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
    

      

  • 相关阅读:
    平台升级至spring 4.3.0 运行稳定
    java过滤特殊字符的正则表达式
    xheditor-文件上传-java-支持html5-application/octet-stream
    java用正则方法验证文件名是否合法
    Java实现在线预览Word,Excel,Ppt文档
    为什么用freemarker视图?
    Java中判断String不为空的问题性能比较
    解决org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource
    Java Swing 使用非本地字体
    第三方包jintellitype实现Java设置全局热键
  • 原文地址:https://www.cnblogs.com/jinzhao/p/2605401.html
Copyright © 2011-2022 走看看