我有两个模型,即登录和注册:
登录模式
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; }
}
他们都在主页上使用单独的模态弹出窗口调用 - >所以,如果用户注册自己(并且在注册时按下输入而不输入电子邮件并显示验证消息)。成功注册后,用户尝试登录(他再次按下输入而不输入电子邮件),但这次没有显示验证消息。
据我所知,未显示验证摘要的原因是验证消息出现在目前不可见的注册模式中,这是因为两个模型都使用电子邮件(名为:Email)字段属性
我可以使用电子邮件字段的不同名称来实现我的愿望行为,但是如果不这样做有什么办法吗?
除此之外我还可以继承Register模型并将其用于登录目的,同时这样做会产生上述场景的输出吗?
答案 0 :(得分:1)
您必须修改编辑器模板以包含前缀或使其独特的内容。这是一个答案,它提供了一些我认为可行的扩展方法TextBoxFor rendering to HTML with prefix on the ID attribute。