I am creating my own helper in MVC. But custom attributes are not added to HTML:
Assistant
public static MvcHtmlString MenuItem(this HtmlHelper helper, string linkText, string actionName, string controllerName, object htmlAttributes) { var currentControllerName = (string)helper.ViewContext.RouteData.Values["controller"]; var currentActionName = (string)helper.ViewContext.RouteData.Values["action"]; var builder = new TagBuilder("li"); if (currentControllerName.Equals(controllerName, StringComparison.CurrentCultureIgnoreCase) && currentActionName.Equals(actionName, StringComparison.CurrentCultureIgnoreCase)) builder.AddCssClass("selected"); if (htmlAttributes != null) { var attributes = new RouteValueDictionary(htmlAttributes); builder.MergeAttributes(attributes, false);
CSHTML
@Html.MenuItem("nossa igreja2", "Index", "Home", new { @class = "gradient-top" })
End result (HTML)
<li class="selected"><a href="/">nossa igreja2</a></li>
Note that he did not add the gradient-top class that I mentioned in the helper call.
source share