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
    

      

  • 相关阅读:
    register based 和 stack based虚拟机的区别
    Java in a Nutshell学习笔记
    Java中interface和abstract class的区别和联系
    Java中final的作用
    Android 源码下载
    Android Fragment 你应该知道的一切
    Android Fragment 真正的完全解析(下)
    Android Fragment 真正的完全解析(上)
    IntelliJ IDEA 使用总结
    Linux在目录中查找某个函数
  • 原文地址:https://www.cnblogs.com/jinzhao/p/2605401.html
Copyright © 2011-2022 走看看