tags:

views:

601

answers:

1

This is my first time trying MVC.

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?

Thanks in advance.

A: 

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>
Richard Nienaber
It doesn't work Object reference not set to an instance of an object.But perhaps that's another error. I'm just really new to MVC. So there is no way to put css class into Html.LabelFor, alright, thanks Richard!
Do what Richard suggested, just create your own HtmlHelper overload of LabelFor(). You can copy the syntax of the LabelFor() source code if you're not sure how to build the expression signature.
Nathan Taylor
Hi Nathan, how do you create an overload for LabelFor? Or where can I copy it from? You can post it as a new answer, thanks.
Just fyi, I know why Model.Name doesn't work. It's because I am adding a new item, so Model (the Item) is still null.