南希访问模型状态在视图中(Nancy access model state in view)

系统教程 行业动态 更新时间:2024-06-14 17:01:34
南希访问模型状态在视图中(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]; });

如果我在验证模型后将用户返回到寄存器视图,如何在那里访问错误? 目前似乎没有任何辅助方法可以输出错误,但我认为可以使用某种字典来访问视图中的错误。 在Nancy的帖子上输出错误的标准究竟是什么?

Right now I am validating the model using nancy data annotations, but I'm not quite sure how to access the model state in the view (using Super Simple View Engine) if the model is not valid, for instance:

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

If I return the user back to the register view after validating the model how can I access the errors there? It doesn't seem like there are any helper methods for outputting errors at the moment, but I figured there would be some sort of dictionary available to access the errors in the view. What exactly is the standard for outputting errors on a post back in Nancy?

最满意答案

您还需要将ModelValidationResult传递回视图,而不仅仅是model 。

You will need to pass ModelValidationResult back to the view as well, instead of just the model.

更多推荐

本文发布于:2023-04-20 18:52:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/c53c136cd63cf3463d6ba8e48c2e4801.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   模型   状态   南希   Nancy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!