I am creating my own helper in MVC. But the custom attributes are not added in the HTML:
Helper
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); //DONT WORK!!!
}
builder.InnerHtml = helper.ActionLink(linkText, actionName, controllerName).ToHtmlString();
return MvcHtmlString.Create(builder.ToString(TagRenderMode.Normal));
}
CSHTML
@Html.MenuItem("nossa igreja2", "Index", "Home", new { @class = "gradient-top" })
Final result (HTML)
<li class="selected"><a href="/">nossa igreja2</a></li>
Note that it did not add the class gradient-top
that I mentioned in the helper call.