Adding DataAnnontations to Generated Partial Classes

后端 未结 3 1357
耶瑟儿~
耶瑟儿~ 2020-12-09 00:02

I have a Subsonic3 Active Record generated partial User class which I\'ve extended on with some methods in a separate partial class.

I would like to know if it is po

相关标签:
3条回答
  • 2020-12-09 00:56

    What you will need to do is create a 'buddy class' and apply the Data Annotations to that class:

    [MetadataType(typeof(UserValidation))]
    public partial class User 
    {
      ...
    }
    
    public class UserValidation
    {
      [DataType(DataType.EmailAddress, ErrorMessage = "Please enter an email address")]
      public string Email { get; set; }
    }
    
    0 讨论(0)
  • 2020-12-09 00:58

    You should create a buddy class as explained here by Scott Guthrie http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx

    0 讨论(0)
  • 2020-12-09 01:03

    This won't directly answer your question, but I had the same problem, and rather than using DataAnnotations, I've been using the FluentValidation framework {0} with great success so far. It works nicely because it provides much of the same functionality, but doesn't apply validation by using attributes on members of the class. Validation happens in a completely separate class that acts on the class being validated (i.e. UserValidator).

    {0}: http://fluentvalidation.codeplex.com/

    0 讨论(0)
提交回复
热议问题