Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set CSS attributes of label in MVC 3

I am creating a static using Html.LabelFor(...). I have to set Name attribute of the label dynamically using JQuery.

like image 768
IrfanRaza Avatar asked Sep 15 '25 00:09

IrfanRaza


1 Answers

You can set the css class, and set inline styles and any other attribute (even non-existant ones like name) using the htmlAttributes parameter provided in one of the overloads of LabelFor

ie

<%: Html.LabelFor(model=>model.Title, 
                           new { style="xyz", @class="abc", @name="MyTitle" }) %>

this would create a label something like:

<label for="Title" style="xyz" class="abc" name="MyTitle">Title</label>

The reason for the @ before class, is that "class" is a reserved word in c#, so you need to qualify it using the @ symbol.

like image 127
James Harris Avatar answered Sep 17 '25 12:09

James Harris



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!