写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,如需转载请自行联系原作者