【发布时间】:2023-03-03 01:41:01
【问题描述】:
我有一个重载 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"。它使用下划线呈现。我想知道这是为什么,如果没有办法转换它?
【问题讨论】:
-
能否添加一个 ReportActionLink 调用的示例?
标签: c# asp.net-mvc