Is there a way to create an ActionLink with HTML 5 Data- attributes?

后端 未结 2 813
[愿得一人]
[愿得一人] 2020-12-29 02:05

HTML5 allows the use of custom attributes prefixed with the phrase \"data-\" which pass validation without the use of a custom DTD (more info). In Asp.Net MVC, is there any

相关标签:
2条回答
  • 2020-12-29 02:36

    or you can use

    new { data_customattribute="value" }

    and the compiler is smart enough to know what you mean

    0 讨论(0)
  • 2020-12-29 02:49

    Yes, there is an overload for ActionLink method which takes an IDictionary<string,object> instead of an anonymous object.

    <%=Html.ActionLink("text", "Index", "Home", null /*routeValues*/, 
        new Dictionary<string, object> { 
           { "data-customattribute", "value" }, 
           { "data-another", "another-value" } 
        })%>
    

    Outputs :

    <a data-another="another-value" data-customattribute="value" href="/">text</a>
    
    0 讨论(0)
提交回复
热议问题