How to use HTML5 data-attributes with Asp.net MVC?

4

How to use attributes with html5 "-" and other plugins with the html asp.net mvc helper?

    
asked by anonymous 02.09.2014 / 01:25

2 answers

1

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.

    
02.09.2014 / 01:54
0

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." } })
    
02.09.2014 / 01:53