开发者

How do I set up the validation of a class across 2 screens?

I have a class, employee, in which the user inputs values for the properties on one screen, and then some more on another screen. The problem I have with this is how to validate these properties? If I put validation attributes for the properties of the class I have a problem. The validation takes place whether the field is displayed on the form or not. So for my Employee class I have had to comment out some of the validation in order to get it to work on 1 screen. It probably won't work on the other. private sealed class Metadata { [HiddenInput(DisplayValue=false)] public int EmployeeId { get; set; }

        [DisplayName("Forename")]
        [DataType(DataType.Text)]
        [Required(ErrorMessage = "Forename is required")]
        public string Forename { get; set; }

        [DisplayName("Surname")]
        [DataType(DataType.Text)]
        [Required(ErrorMessage = "Surname is required")]
        public string Surname { get; set; }

        [DisplayName("Middle Names")]
        [DataType(DataType.Text)]
        public string Middlenames { get; set; }

        //[DisplayName("User Name")]
        //[DataType(DataType.Text)]
        //[Required(ErrorMessage = "User name is required")]
        //public string UserName { get; set; }

        [DisplayName("Employee Number")]
        [DataType(DataType.Text)]
        [Required(ErrorMessage = "EmployeeNumber is required")]
        public string EmployeeNumber { get; set; }

        [DisplayName("Department")]
        [UIHint("DropDownList")]
        [Required(ErrorMessage = "You must select a department from a division")]
        public int DepartmentId { get; set; }

        [DisplayName("User Role")]
        [UIHint("DropDownList")]
        [Required(ErrorMessage = "You must select a role")]
        public int SHP_UserRoleId { get; set; }

        //[DisplayName("Email")]
        //[DataType(DataType.EmailAddress)]
        //[Required(ErrorMessage = "Email is required")]
        //[RegularExpression(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$", ErrorMessage = "Not a valid email")]
        //[UniqueEmail(ErrorMessage = "User already exists")]
        //public string EmailAddress { get; set; }

        [DisplayName("End Date")]
        pub开发者_运维技巧lic DateTime? EndDate { get; set; }
    }


That's a common problem people encounter when they try to use their business models in the views and the reason for this is that business models are closer to the business and the view is closer to the application (it's just a representation of this business model). Today you have two screens, tomorrow maybe three.

For this reason I would recommend you using view models which reflect a given view. So in your case you could have two view models for each view and populate them from the same business model. Validation attributes could be placed on view models. To avoid boilerplate code in converting between your business models and your view models you could use AutoMapper.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜