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

  

posted @   today4king  阅读(938)  评论(0编辑  收藏  举报
编辑推荐:
· 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 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示
主题色彩