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" にはなりません。アンダースコアでレンダリングされます。これがなぜなのか、変換する方法がないのだろうか?