How to use attributes with html5 "-" and other plugins with the html asp.net mvc helper?
How to use attributes with html5 "-" and other plugins with the html asp.net mvc helper?
It has a parameter called htmlAttributte
of type object
, practically all the components that I know have this parameter available in its constructor, with it you can set the html
attributes of the component, such as label
:
@Html.LabelFor(model => model.Nome, new { @class = "control-label", @data_time = "teste" })
The only restriction on its use is that you should not use the "-" but "_" as a separation. As quoted here .
The above example will be rendered in the browser like this:
<label class="control-label" data-time="teste" for="Nome">Nome</label>
I believe this will help resolve your issue.
Create a IDictionary , which represents a structure of value pair where the elements could be the options of helpers
of MVC ASP.NET
@{
IDictionary<string, object> options = new Dictionary<string, object>();
options.Add("data-val", "true");
options.Add("data-val-required", "O campo Dias é obrigatório.");
}
Call it like this:
@Html.TextBoxFor(x => x.Dias, options)
Or you can call it that way too:
@Html.TextBoxFor(x => x.Dias, new Dictionary<string, object> { { "data-val", "true" }, { "data-val-required", "O campo Dias é obrigatório." } })