Nancy 在视图中访问模型状态
Nancy access model state in view
现在我正在使用 nancy 数据注释验证模型,但我不太确定如果模型无效如何访问视图中的模型状态(使用超级简单视图引擎),例如:
Post("/register", async args =>
{
ViewBag.ReturnUrl = Request.Query.returnUrl;
ViewBag.LoginProviders = _signInManager.GetExternalAuthenticationSchemes().ToList();
var model = this.BindAndValidate<RegisterViewModel>();
if (ModelValidationResult.IsValid)
{
var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
var result = await _userManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
var adminUsers = await _userManager.GetUsersInRoleAsync("admin");
if (!adminUsers.Any())
{
if (!await _roleManager.RoleExistsAsync("admin"))
{
var roleResult = await _roleManager.CreateAsync(new IdentityRole
{
Name = "admin",
NormalizedRoleName = "admin"
});
if (roleResult.Succeeded)
{
await _userManager.AddToRoleAsync(user, "admin");
}
}
else
{
await _userManager.AddToRoleAsync(user, "admin");
}
}
await _signInManager.SignInAsync(user, isPersistent: false);
Logger.LogInformation(3, "User created a new account with password.");
return Response.AsRedirect("~/");
}
}
return View["register.html", model];
});
如果我 return 用户在验证模型后返回到注册视图,我该如何访问那里的错误?目前似乎没有任何用于输出错误的辅助方法,但我认为会有某种字典可用于访问视图中的错误。 post back in Nancy 的错误输出标准是什么?
您还需要将 ModelValidationResult
传回视图,而不仅仅是 model
。
现在我正在使用 nancy 数据注释验证模型,但我不太确定如果模型无效如何访问视图中的模型状态(使用超级简单视图引擎),例如:
Post("/register", async args =>
{
ViewBag.ReturnUrl = Request.Query.returnUrl;
ViewBag.LoginProviders = _signInManager.GetExternalAuthenticationSchemes().ToList();
var model = this.BindAndValidate<RegisterViewModel>();
if (ModelValidationResult.IsValid)
{
var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
var result = await _userManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
var adminUsers = await _userManager.GetUsersInRoleAsync("admin");
if (!adminUsers.Any())
{
if (!await _roleManager.RoleExistsAsync("admin"))
{
var roleResult = await _roleManager.CreateAsync(new IdentityRole
{
Name = "admin",
NormalizedRoleName = "admin"
});
if (roleResult.Succeeded)
{
await _userManager.AddToRoleAsync(user, "admin");
}
}
else
{
await _userManager.AddToRoleAsync(user, "admin");
}
}
await _signInManager.SignInAsync(user, isPersistent: false);
Logger.LogInformation(3, "User created a new account with password.");
return Response.AsRedirect("~/");
}
}
return View["register.html", model];
});
如果我 return 用户在验证模型后返回到注册视图,我该如何访问那里的错误?目前似乎没有任何用于输出错误的辅助方法,但我认为会有某种字典可用于访问视图中的错误。 post back in Nancy 的错误输出标准是什么?
您还需要将 ModelValidationResult
传回视图,而不仅仅是 model
。