Defining html5 data attribute in MVC

I am trying to declare an Html.Radio button in my mvc application and want to output the data attribute. The problem is that C # matters "-"

<%= Html.RadioButton("defaultRadioButton", d.Link, d.IsDefault, new { data-link = d.Link })%>

Is there any way around this other than outputting html itself or creating an assistant?

Thank..

+3
source share
1 answer

If this is ASP.NET MVC 3:

<%= Html.RadioButton(
    "defaultRadioButton", 
    d.Link, 
    d.IsDefault, 
    new { 
        data_link = d.Link 
    }
)%>

and the underscore will be automatically converted to dash by helper.

In previous versions of MVC, an ugly hack may be launched:

<%= Html.RadioButton(
    "defaultRadioButton", 
    d.Link, 
    d.IsDefault, 
    new Dictionary<string, object> { 
        { "data-link", d.Link } 
    }
) %>
+11
source

Source: https://habr.com/ru/post/1794117/


All Articles