Model view controller 将类添加到Html.MenuItem';s
我有一个由MVC2生成的菜单项的HTML列表Model view controller 将类添加到Html.MenuItem';s,model-view-controller,asp.net-mvc-2,asp.net-mvc,Model View Controller,Asp.net Mvc 2,Asp.net Mvc,我有一个由MVC2生成的菜单项的HTML列表 <%= Html.MenuItem("Home", "Home", "Index")%> <%= Html.MenuItem("Login", "Login", "Account")%> 在HTML中生成: <li> <a href="Index">Home</a> </li> <li> <a href="Account">Login<
<%= Html.MenuItem("Home", "Home", "Index")%>
<%= Html.MenuItem("Login", "Login", "Account")%>
在HTML中生成:
<li>
<a href="Index">Home</a>
</li>
<li>
<a href="Account">Login</a>
</li>
如何为列表中的每个项目向列表中的元素添加CSS类?我猜此MenuItem是您编写的或从某人处获取的扩展方法,我猜他们正在包装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>");
}
publicstaticstringmenuitem(此HtmlHelper帮助程序、stringlinktext、stringactionName、stringcontrollerName)
{
string currentControllerName=(string)helper.ViewContext.RoutedData.Values[“controller”];
string currentActionName=(string)helper.ViewContext.RoutedData.Values[“action”];
//添加所选类
if(currentControllerName.Equals(controllerName,StringComparison.CurrentCultureInoRecase)和¤tActionName.Equals(actionName,StringComparison.CurrentCultureInoRecase))
返回string.Concat(“”,helper.ActionLink(linkText,actionName,controllerName),“ ”;
//添加链接
返回字符串.Concat(“”,helper.ActionLink(linkText,actionName,controllerName),“ ”;
}
如果是这种情况,只需将一个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>");
}
publicstaticstringmenuitem(此HtmlHelper帮助程序、stringlinktext、stringactionName、stringcontrollerName、stringcssclass=“菜单项”)
{
string currentControllerName=(string)helper.ViewContext.RoutedData.Values[“controller”];
string currentActionName=(string)helper.ViewContext.RoutedData.Values[“action”];
//添加所选类
if(currentControllerName.Equals(controllerName,StringComparison.CurrentCultureInoRecase)和¤tActionName.Equals(actionName,StringComparison.CurrentCultureInoRecase))
返回string.Concat(“”,helper.ActionLink(linkText,actionName,controllerName),“ ”;
//添加链接
返回string.Concat(“”,helper.ActionLink(linkText,actionName,controllerName,new{@class=cssClass}),“ ”;
}
然后你就这样称呼他们:
<%= Html.MenuItem("Home", "Home", "Index", "index-tem")%>
我猜此MenuItem是您编写的或从某人处获取的扩展方法,我还猜他们正在包装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>");
}
publicstaticstringmenuitem(此HtmlHelper帮助程序、stringlinktext、stringactionName、stringcontrollerName)
{
string currentControllerName=(string)helper.ViewContext.RoutedData.Values[“controller”];
string currentActionName=(string)helper.ViewContext.RoutedData.Values[“action”];
//添加所选类
if(currentControllerName.Equals(controllerName,StringComparison.CurrentCultureInoRecase)和¤tActionName.Equals(actionName,StringComparison.CurrentCultureInoRecase))
返回string.Concat(“”,helper.ActionLink(linkText,actionName,controllerName),“ ”;
//添加链接
返回字符串.Concat(“”,helper.ActionLink(linkText,actionName,controllerName),“ ”;
}
如果是这种情况,只需将一个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>");
}
publicstaticstringmenuitem(此HtmlHelper帮助程序、stringlinktext、stringactionName、stringcontrollerName、stringcssclass=“菜单项”)
{
string currentControllerName=(string)helper.ViewContext.RoutedData.Values[“controller”];
string currentActionName=(string)helper.ViewContext.RoutedData.Values[“action”];
//添加所选类
if(currentControllerName.Equals(controllerName,StringComparison.CurrentCultureInoRecase)和¤tActionName.Equals(actionName,StringComparison.CurrentCultureInoRecase))
返回string.Concat(“”,helper.ActionLink(linkText,actionName,controllerName),“ ”;
//添加链接
返回string.Concat(“”,helper.ActionLink(linkText,actionName,controllerName,new{@class=cssClass}),“ ”;
}
然后你就这样称呼他们:
<%= Html.MenuItem("Home", "Home", "Index", "index-tem")%>