mat-mcloughlin
mat-mcloughlin

Reputation: 6722

Defining a html5 data attribute in MVC

I'm trying to declare a Html.Radio button in my mvc app and want to output a data- attribute. Problem is c# does like "-"

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

Is there anyway to get round this other than outputting the html myself or creating a helper?

Thanks..

Upvotes: 3

Views: 2396

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038890

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 into a dash by the helper.

In previous versions of MVC an ugly hack could be appiled:

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

Upvotes: 11

Related Questions