I have a function that overloads actionlink and simply adds a new parameter to the route values "ID", which I'm using all over the place.
Here is my code so far:
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);
}
As you can see, I pass in the routeValues, convert them to a dictionary and add my ID.
The problem occurs when I convert my htmlAttributes to and IDictionary because the overloaded method expects that, it doesn't replace the underscores in my properties, ie
data_event = "something" does NOT become data-event = "something" as it does with the anonymous type. It renders with the underscore. I wonder why this is and if there isn't a way to convert it?
You just need to pass the htmlAttributes
object through HtmlHelper.AnonymousObjectToHtmlAttributes
instead of using the provided RouteValueDictionary
constructor.
From MSDN:
Return Value
Type: System.Web.Routing.RouteValueDictionary
The HTML attributes with underscore characters replaced by hyphens.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.