我猜這個菜單項是你寫的擴展方法,或者你從別人拿了,我也猜測,他們正在包裝一個ActionLink的方法,如:
public static string MenuItem(this HtmlHelper helper, string linkText, string actionName, string controllerName)
{
string currentControllerName = (string)helper.ViewContext.RouteData.Values["controller"];
string currentActionName = (string)helper.ViewContext.RouteData.Values["action"];
// Add selected class
if (currentControllerName.Equals(controllerName, StringComparison.CurrentCultureIgnoreCase) && currentActionName.Equals(actionName, StringComparison.CurrentCultureIgnoreCase))
return string.Concat("<li class=\"selected\">", helper.ActionLink(linkText, actionName, controllerName), "</li>");
// Add link
return string.Concat("<li>", helper.ActionLink(linkText, actionName, controllerName), "</li>");
}
,如果是這樣的話只是使它採取的是一個CSS類作爲參數,並使用該發生在htmlattributes,如ActionLink的:
public static string MenuItem(this HtmlHelper helper, string linkText, string actionName, string controllerName, string cssClass = "menu-item")
{
string currentControllerName = (string)helper.ViewContext.RouteData.Values["controller"];
string currentActionName = (string)helper.ViewContext.RouteData.Values["action"];
// Add selected class
if (currentControllerName.Equals(controllerName, StringComparison.CurrentCultureIgnoreCase) && currentActionName.Equals(actionName, StringComparison.CurrentCultureIgnoreCase))
return string.Concat("<li class=\"selected\">", helper.ActionLink(linkText, actionName, controllerName), "</li>");
// Add link
return string.Concat("<li>", helper.ActionLink(linkText, actionName, controllerName, new {@class = cssClass}), "</li>");
}
那麼你只需要打電話給他們這樣的:
<%= Html.MenuItem("Home", "Home", "Index", "index-tem")%>