.NET MVC - How to assign a class to Html.LabelFor?

Viewed 69309

This code

<%= Html.LabelFor(model => model.Name) %>

produces this

<label for="Name">Name</label>

But I want this

<label for="Name" class="myLabel">Name</label>

How do you do that?

3 Answers

Okay, looking at the source (System.Web.Mvc.Html.LabelExtensions.cs) for this method, there doesn't seem to be a way to do this with an HtmlHelper in ASP.NET MVC 2. I think your best bet is to either create your own HtmlHelper or do the following for this specific label:

<label for="Name" class="myLabel"><%= Model.Name %></label>
Related