开发者

Validation messages not showing while using same named attributes in the Model?

开发者 https://www.devze.com 2023-03-07 13:21 出处:网络
I have two models i.e. Login and Register: Login Model public class LoginModel { [Required(ErrorMessage = \"Email is required\")]

I have two models i.e. Login and Register:

Login Model

public class LoginModel
    {
        [Required(ErrorMessage = "Email is required")]
        [RegularExpression("^[a-z0-9_\\+-]+(\\.[a-z0-9_\\+-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*\\.([a-z]{2,4})$", ErrorMessage = "Not a valid email")]
        [DataType(DataType.EmailAddress)]
        [DisplayName("Email")]
        [StringLength(150, ErrorMessage = "Must be less than 150 characters")]
        public string Email { get; set; }

        [Required(ErrorMessage = "Password is required")]
        [DataType(DataType.Password)]
        [DisplayName("Password")]
        [StringLength(30, ErrorMessage = "Must be less than 30 characters")]
        public string LoginPassword { get; set; }

        [DisplayName("Remember me")]
        public bool Remember { get; set; }

    }

Register Model:

   public class RegisterModel
    {
        [Required(ErrorMessage = "Email is required")]
        [RegularExpression("^[a-z0-9_\\+-]+(\\.[a-z0-9_\\+-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*\\.([a-z]{2,4})$", ErrorMessage = "Not a valid email")]
        [DataType(DataType.EmailAddress)]
        [DisplayName("Email")]
        [StringLength(150, ErrorMessage = "Must be less than 150 characters")]
        public string Email { get; set; }

        [Required(ErrorMessage = "Full Name is required")]
        [DisplayName("Full Name")]
        [StringLength(50, ErrorMessage = "Must be less than 50 characters")]
        public string FullName { get; set; }

        [Required(ErrorMessage = "Password is required")]
        [DataType(DataType.Password)]
        [DisplayName("Password")]
        [StringLength(30, ErrorMessage = "Must be less than 30 characters")]
        public string RegisterPassword { get; set; }

        [Required(ErrorMessage = "Confirm Password is required")]
        [DataType(DataType.Password)]
        [DisplayName("Confirm Password")]
        [StringLength(30, ErrorMessage = "Must be less than 30 characters")]
        public string ConfirmPassword { get; set; }

        [Required(ErrorMessage = "Please read and agree the terms and condition.")]
        [DisplayName("I agree to the terms and conditions")]
        public bool AgreeTerms { get; set; }

    }

And they both are called on the home page using a seperate modal popups --> so lets say if a user register himself (and while registering he presses enter without entering email and validation message is displayed). After successfully registering, user tries to login (and he again presses the enter without entering email) but this time the validation message is not displayed.

As per my knowledge, the reason for not showing the validation summary is that the validation message is appearing in the register modal which is not visible at the moment and it is because both models uses an email (named : Email) field attr开发者_StackOverflow中文版ibute.

I can achieve my desire behaviour using different name for email field, but is there any way without doing this ???

And addition to this can I inherit from Register model and use it for login purpose also, while doing this what will be the output of my above scenario?


You'd have to modify the editor templates to include a prefix or something making them unique. Here's an answer that provides some extension methods that I think would work TextBoxFor rendering to HTML with prefix on the ID attribute .

0

精彩评论

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

关注公众号