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

Asked

Viewed 462 times

4

How to use attributes with " - " from Html5 and other plugins with the Html helper from Asp.net mvc?

2 answers

1

Has a parameter called htmlAttributte of the kind object, virtually every component I know has this parameter available in your constructor, with it you can set the attributes html of the component, such as a label:

@Html.LabelFor(model => model.Nome, new { @class = "control-label", @data_time = "teste" })

The only restriction of its use is that you should not use the "-" but "_" as a separation. As cited 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 solve your problem.

0

Create a Idictionary, that represents an even value structure where the elements could be the options of the 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 that:

@Html.TextBoxFor(x => x.Dias, options)

Or you can call it straight as well:

@Html.TextBoxFor(x => x.Dias, new Dictionary<string, object> { { "data-val", "true" }, { "data-val-required", "O campo Dias é obrigatório." } })

Browser other questions tagged

You are not signed in. Login or sign up in order to post.