Html5 data-* with asp.net mvc TextboxFor html 属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4844001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 06:24:59  来源:igfitidea点击:

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

asp.net-mvchtmlasp.net-mvc-3

提问by Shawn Mclean

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

如何data-*使用 TextboxFor添加html 属性?

This is what I currently have:

这是我目前拥有的:

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

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

如您所见,这-在这里造成了问题data-url。解决这个问题的方法是什么?

回答by Darin Dimitrov

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:

对于那些想要在 ASP.NET MVC 3 之前的版本中实现相同目标的人,他们可以:

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