views:

241

answers:

2

I'm using ASP.NET MVC2 and Data Annotations.

I've decorated a property in my buddy class with the Required attribute.

Is there a way to get the Html.LabelFor() helper method to automatically display an asterix to signify that the field is required?

Only ways I can think of to do this are:

a) Extend LabelExtensions

Or

b) Manually add the asterix to the DisplayName attribute, e.g. "My Field *"

The latter is simplest, but introduces a dependency between the DisplayName and Required attributes.

Any other suggestions?

A: 

You need to create your own default templates. This article series by Brad Wilson explains how to do this:

http://bradwilson.typepad.com/blog/2009/04/dataannotations-and-aspnet-mvc.html

jfar
A: 

Instead of using the Html.LabelFor, you should create your own view helper which will create the label and insert the * is appropriate. You can find detailed info about how to create a new view helper (which is very easy) at http://mvcviewhelpers.codeplex.com/.

Sayed Ibrahim Hashimi
can the down-voter mention why? It would be helpful for those coming along later. Cheers.
Myster