Html Helper with special htmlattributes

2019-08-11 20:24发布

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

3条回答
Explosion°爆炸
2楼-- · 2019-08-11 20:51

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" })
查看更多
Melony?
3楼-- · 2019-08-11 20:55

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" })
查看更多
老娘就宠你
4楼-- · 2019-08-11 21:04

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" }
查看更多
登录 后发表回答