Html Helper with special htmlattributes

2019-08-11 20:02发布

问题:

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