Reputation: 665
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?
Upvotes: 3
Views: 3461
Reputation: 44342
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/.
Upvotes: 3
Reputation: 24754
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
Upvotes: 0