在模型中使用相同命名的属性时,验证消息未显示?

发布于 2024-11-08 14:17:21 字数 2557 浏览 0 评论 0原文

我有两个模型,即登录和注册:

登录模型

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; }

    }

注册模型:

   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; }

    }

它们都在主页上使用单独的模式弹出窗口调用 -->假设用户自己注册(并且在注册时按 Enter 键而不输入电子邮件并显示验证消息)。成功注册后,用户尝试登录(他再次按回车键而不输入电子邮件),但这次不显示验证消息。

据我所知,不显示验证摘要的原因是验证消息出现在注册模式中,目前不可见,这是因为两个模型都使用电子邮件(名为:Email)字段属性。

我可以使用电子邮件字段的不同名称来实现我的愿望行为,但是有什么方法可以不这样做???

除此之外,我可以继承 Register 模型并将其用于登录目的,同时执行此操作,上述场景的输出是什么?

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 attribute.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

触ぅ动初心 2024-11-15 14:17:21

您必须修改编辑器模板以包含前缀或使它们独一无二的东西。这是一个答案,提供了一些我认为可行的扩展方法 TextBox用于渲染为 HTML,并在 ID 属性上添加前缀

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 .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文