How does DataAnnotations really work in MVC?

MVC3 has a new jQuery Validation mechanism that link jQuery Validation and Validation Attributes Metadata, this is the jquery.validate.unobtrusive file that takes all data- attributes and work with them, just like before when you set the <add key=”UnobtrusiveJavaScriptEnabled” value=”false” /> All you need to do is come up with your own Custom Validation Attributes, for … Read more

How do I specify that a property should generate a TEXT column rather than an nvarchar(4000)

I appreciate the effort that went into the existing answer, but I haven’t found it actually answering the question… so I tested this, and found out that [Column(TypeName = “ntext”)] public string Body { get; set; } (the one from System.ComponentModel.DataAnnotations) will work to create an ntext type column. (My problem with the accepted answer … Read more

Data Annotations for validation, at least one required field?

I have extended Zhaph answer to support grouping of properties. [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] public class AtLeastOnePropertyAttribute : ValidationAttribute { private string[] PropertyList { get; set; } public AtLeastOnePropertyAttribute(params string[] propertyList) { this.PropertyList = propertyList; } //See http://stackoverflow.com/a/1365669 public override object TypeId { get { return this; } } public override bool IsValid(object value) { … Read more

Conditionally required property using data annotations

RequiredIf validation attribute I’ve written a RequiredIfAttribute that requires a particular property value when a different property has a certain value (what you require) or when a different property has anything but a specific value. This is the code that may help: /// <summary> /// Provides conditional validation based on related property value. /// </summary> … Read more

displayname attribute vs display attribute

DisplayName sets the DisplayName in the model metadata. For example: [DisplayName(“foo”)] public string MyProperty { get; set; } and if you use in your view the following: @Html.LabelFor(x => x.MyProperty) it would generate: <label for=”MyProperty”>foo</label> Display does the same, but also allows you to set other metadata properties such as Name, Description, … Brad Wilson … Read more