使用数据注释部分验证模型 属性
Partially validate model property using data annotation
我已经使用数据注释对模型 属性 设置了两个验证,如下所示:
[MinLength(8, ErrorMessage = "Password Requires at least one letter, one number and 8 characters long")]
[Required(ErrorMessage = "Password Required")]
public string Password { get; set; }
我想在某些特殊情况下进行部分验证。例如,我不想在用户登录时检查最小长度,而只是在注册时检查。
任何人都知道如何实现这一目标吗?
注册和登录使用不同的 ViewModel,就像默认的 asp.net-mvc 实现一样。
你最终会得到 3 个 classes:你的模型本身,一个登录 class 和一个注册 class。唯一具有密码长度验证的 class 应该是模型本身,而不是视图模型。使用您的控制器,然后您应该从 ViewModel 填充到模型(在执行 Posts 时)或从模型填充到 ViewModel(在执行 Gets 时)
登录 ViewModel 示例(取自默认 MVC 代码)
public class LoginViewModel
{
[Required]
[Display(Name = "Email")]
[EmailAddress]
public string Email { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[Display(Name = "Remember me?")]
public bool RememberMe { get; set; }
}
还有一个 Register ViewModel,也是来自默认的 MVC
public class RegisterViewModel
{
[Required]
[EmailAddress]
[Display(Name = "Email")]
public string Email { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
}
Register ViewModel 和模型本身的使用示例,所有默认 MVC
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);
// For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
// Send an email with this link
// string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
// var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
// await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");
return RedirectToAction("Index", "Home");
}
AddErrors(result);
}
// If we got this far, something failed, redisplay form
return View(model);
}
我已经使用数据注释对模型 属性 设置了两个验证,如下所示:
[MinLength(8, ErrorMessage = "Password Requires at least one letter, one number and 8 characters long")]
[Required(ErrorMessage = "Password Required")]
public string Password { get; set; }
我想在某些特殊情况下进行部分验证。例如,我不想在用户登录时检查最小长度,而只是在注册时检查。
任何人都知道如何实现这一目标吗?
注册和登录使用不同的 ViewModel,就像默认的 asp.net-mvc 实现一样。
你最终会得到 3 个 classes:你的模型本身,一个登录 class 和一个注册 class。唯一具有密码长度验证的 class 应该是模型本身,而不是视图模型。使用您的控制器,然后您应该从 ViewModel 填充到模型(在执行 Posts 时)或从模型填充到 ViewModel(在执行 Gets 时)
登录 ViewModel 示例(取自默认 MVC 代码)
public class LoginViewModel
{
[Required]
[Display(Name = "Email")]
[EmailAddress]
public string Email { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[Display(Name = "Remember me?")]
public bool RememberMe { get; set; }
}
还有一个 Register ViewModel,也是来自默认的 MVC
public class RegisterViewModel
{
[Required]
[EmailAddress]
[Display(Name = "Email")]
public string Email { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
}
Register ViewModel 和模型本身的使用示例,所有默认 MVC
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);
// For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
// Send an email with this link
// string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
// var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
// await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");
return RedirectToAction("Index", "Home");
}
AddErrors(result);
}
// If we got this far, something failed, redisplay form
return View(model);
}