Html5 data-* with asp.net mvc TextboxFor html 属性 [英] Html5 data-* with asp.net mvc TextboxFor html attributes

查看:32
本文介绍了Html5 data-* with asp.net mvc TextboxFor html 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这是我目前拥有的:

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

如您所见,-data-url 处引起了问题.有什么办法解决这个问题?

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") }
)

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

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") } 
    }
) %>

这篇关于Html5 data-* with asp.net mvc TextboxFor html 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆