Html Helper with special htmlattributes

南楼画角 提交于 2020-01-06 03:48:04

问题


Does anyone know how to pass html attributes like "data-date" to the Html.TextBox()??

It is used from the bootstrap datepicker.

Is there a way around it?

            @Html.TextBoxFor(p => p.DateFrom, new { @class = "small", type = "date", "data-date"="12-02-2012" })

回答1:


You have to use underscore instead of '-'.

So what you're after would go something like this:

@Html.TextBoxFor(p => p.DateFrom, new { @class = "small", @type = "date", @data_date="12-02-2012" })



回答2:


Use:

new Dictionary<string, object> { { "class", "small" }, {"type", "date"} { "data-date", "12-02-2012" } }

instead of:

new { @class = "small", type = "date", "data-date"="12-02-2012" }



回答3:


I was trying to add the date picker to the Html helper using bootstrap. rudeovski ze bear's answer made it easier.

This is how it worked for me.

@Html.TextBox("startdate", null
                 , new {@class = "type", @type = "date", @data_date="12-02-2012" })


来源:https://stackoverflow.com/questions/9917596/html-helper-with-special-htmlattributes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!