I am trying to extend Html.ActionLink as I want to add custom metadata for a generic component (in this case modal).
My goal is to extend the LinkExtensions class inside .Net MVC, which will add a value to the html class attribute and add a user data attribute, resulting in the following:
<a href="/Controller/Action/id" class="show-in-modal style1 style2" data-title="Modal title">Link</a>
The helper will be similar to the MVC method:
public static MvcHtmlString ModalLink(this HtmlHelper htmlHelper, string title, string linkText, string actionName, string controllerName, object routeValues, object htmlAttributes)
{
return htmlHelper.ActionLink(linkText, actionName, controllerName, routeValues, htmlAttributes);
}
@Html.ModalLink("Modal title", "Link", "action", "controller", new { id = "id" }, new { @class = "style1 style2" });
For this problem, I cannot easily modify the htmlAttributes object to add an attribute of the name and data of the class, which makes sense since it is an anonymous read-only object.
Is there a way that I can easily apply the required values / metadata without having to break everything with reflection and merge again?
, MVC , html IDictionary<string, object>, , ?
, , - Html.ActionLink().