开发者

Html5 data-* with asp.net mvc TextboxFor html attributes

开发者 https://www.devze.com 2023-02-07 07:21 出处:网络
How do I add data-* html attributes using TextboxFor? This is what I currently have: @Html.TextBoxFor(model => model.Country.CountryName, new { data-url开发者_C百科= Url.Action(\"CountryContains\

How do I add data-* html attributes using TextboxFor?

This is what I currently have:

@Html.TextBoxFor(model => model.Country.CountryName, new { data-url开发者_C百科= Url.Action("CountryContains", "Geo") })

As you see, the - is causing a problem here data-url. Whats the way around this?


You could use underscore (_) and the helper is intelligent enough to do the rest:

@Html.TextBoxFor(
    model => model.Country.CountryName, 
    new { data_url = Url.Action("CountryContains", "Geo") }
)

And for those who want to achieve the same in pre ASP.NET MVC 3 versions they could:

<%= Html.TextBoxFor(
    model => model.Country.CountryName, 
    new Dictionary<string, object> { 
        { "data-url", Url.Action("CountryContains", "Geo") } 
    }
) %>
0

精彩评论

暂无评论...
验证码 换一张
取 消