Nation
Nation

Reputation: 506

How do I add attributes to an element created with razor helpers?

I am trying to add attributes like class, style .etc, using the razor helpers. How can I accomplish this?

Example: @Html.LabelFor(model => model.First)

Upvotes: 3

Views: 2040

Answers (2)

Botonomous
Botonomous

Reputation: 1764

You need to use the New keyword and you can add HTML attributes like so:

@Html.LabelFor(model => model.First, new { @class = "test", id = "Lbl1"}))

Etc..You can keep adding HTML attributes inside the new {}.

Upvotes: 4

Cristi Pufu
Cristi Pufu

Reputation: 9095

@Html.LabelFor(model => model.First, new Dictionary<string, object>() { { "class", "class-name" }, { "style", "display:none" } });

Is that what u mean ?

Upvotes: 2

Related Questions