Correctly making an ActionLink extension with htmlAttributes

后端 未结 1 1185
栀梦
栀梦 2021-01-07 10:51

I use a custom extension for my ActionLinks. I have added an attribute data_url which is meant to be translated to an attribute of data-url. This i

相关标签:
1条回答
  • 2021-01-07 11:19

    You could use the AnonymousObjectToHtmlAttributes method which does exactly what you want and you don't need any custom extension methods:

    public static MvcHtmlString ActionLink(
        this AjaxHelper helper, 
        string linkText, 
        RouteValueDictionary routeValues, 
        AjaxOptions ajaxOptions, 
        object htmlAttributes = null
    )
    {
        return helper.ActionLink(
            linkText,    
            routeValues["Action"].ToString(), 
            routeValues["Controller"].ToString(), 
            routeValues, 
            ajaxOptions, 
            htmlAttributes == null ? null : HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)
        );
    }
    
    0 讨论(0)
提交回复
热议问题