2011-01-30 5 views
221

Comment ajouter data-* attributs html en utilisant TextboxFor?Données Html5- * avec asp.net mvc Attributs TextboxFor html

C'est ce que j'ai actuellement:

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

Comme vous le voyez, le - pose un problème ici data-url. Quel est le moyen de contourner cela?

+0

double possible de [attributs html avec Hyphenated asp.net mvc] (http://stackoverflow.com/questions/2897733/hyphenated-html-attributes-with-asp-net-mvc) –

Répondre

373

Vous pouvez utiliser underscore (_) et l'aide est assez intelligent pour faire le reste:

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

Et pour ceux qui veulent obtenir le même dans ASP.NET MVC pré 3 versions, ils pourraient:

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

Est-ce comportement spécifié n'importe où :)? – Rookian

+0

@Rookian, je n'ai aucune idée si elle est spécifiée n'importe où. Je suppose que certains billets de blog sur internet devraient en parler. –

+0

Ne fonctionne pas pour 'EditorFor' – Ryan

Questions connexes