Palantir
Palantir

Reputation: 24182

MVC2, data annotation with Entities?

Is there any way to use an Entity as a model, and be able to decorate its fields in order to get validation? I am using the Entity as a model for convenience, but then I need to do all the validation manually...

Upvotes: 0

Views: 383

Answers (1)

Palantir
Palantir

Reputation: 24182

Found it!

Say Course is the entity.

You then implement these two:

namespace MyNamespace.Models {
  [MetadataType(typeof(CourseMetadata))]
  public partial class Course {

  }

  public class CourseMetadata {
    [DisplayName("Course location")]
    [Required]
    public string place {get; set;}
  }
}

The first declares that the metadata for Course are taken from CourseMetadata, the second one contains the same fields as the Course, with the only purpose to inject metadata onto them. Cool :D

Upvotes: 2

Related Questions