繁体   English   中英

在MVC和htmlAttributes中重载ActionLink

[英]Overloading ActionLink in MVC and htmlAttributes

我有一个重载actionlink的函数,只是在路由值“ID”中添加一个新参数,我在整个地方使用它。

到目前为止,这是我的代码:

public static MvcHtmlString ReportActionLink(this HtmlHelper helper, string linkText, string actionName, string controllerName, object routeValues, object htmlAttributes)
{
    RouteValueDictionary routeValueDictionary = new RouteValueDictionary(routeValues);
    routeValueDictionary.Add("id", HttpContext.Current.Request.RequestContext.RouteData.Values["id"]);

    IDictionary<string, object> attrs = new RouteValueDictionary(htmlAttributes);

    return helper.ActionLink(linkText, actionName, controllerName, routeValueDictionary, attrs);        
}

如您所见,我传入routeValues,将它们转换为字典并添加我的ID。

当我将htmlAttributes转换为IDictionary时会出现问题,因为重载方法需要这样做,它不会替换我的属性中的下划线,即

data_event =“something”不会像匿名类型那样成为data-event =“something”。 它以下划线呈现。 我想知道为什么会这样,如果没有办法转换呢?

您只需要通过HtmlHelper.AnonymousObjectToHtmlAttributes传递htmlAttributes对象,而不是使用提供的RouteValueDictionary构造函数。

来自MSDN:

回报价值
键入: System.Web.Routing.RouteValueDictionary
带有下划线字符的HTML属性由连字符替换。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM