![](/img/trans.png)
[英]Html.ValidationSummary Not Populating with Custom Errors ASP.NET MVC Razor Page
[英]Asp.net MVC5 Why won't Html.ValidationSummary(false) display exception errors?
我正在嘗試讓我正在建立的asp.net mvc5網站上的用戶帳戶管理工作。 新用戶的創建基本上可以正常工作,但是在創建用戶期間引發異常時,顯示錯誤會出現問題。 捕獲/處理了異常,並且在我的AdminController的Create()操作中向ModelState對象添加了錯誤消息,但是當我返回到Create視圖時,不會顯示該錯誤。
不會導致引發異常的其他類型的用戶創建錯誤可以正確顯示。 附帶說明,發生問題時,瀏覽器上的“旋轉器”永遠不會停止,這表明發起create動作的帖子從未完成。 如果單擊瀏覽器中的菜單,則導航到新頁面,微調框消失。
在以下情況下會出現正確顯示且與異常無關的Create()錯誤:
var newUserCreateResult = UserManager.Create(newUser, password)
返回:
newUserCreateResult.Succeeded == false
與異常相關聯且無法正確顯示的create()錯誤在以下情況下發生:
await UserManager.SendEmailAsync(newUser.Id, "Reset Password", "Please confirm your account and reset your password by clicking <a href=\"" + callbackUrl + "\">here</a>");
引發異常。 我使用的是SmtpMailService,當無法訪問電子郵件服務器或smtp帳戶配置不正確時,將引發異常。
我嘗試過的一些事情:
我的Create()動作:
// PUT: /Admin/Create
[Authorize(Roles = "Admin")]
[HttpPost]
[ValidateAntiForgeryToken]
#region public ActionResult Create(CreateUserDTO createUserDTO)
public async Task<ActionResult> Create(CreateUserDTO createUserDTO) {
ApplicationUser newUser = null;
try {
if (createUserDTO == null) {
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
var Email = createUserDTO.Email.Trim();
if (Email == "") {
throw new Exception("No Email");
}
// Create user
newUser = new ApplicationUser { UserName = Email, Email = Email };
var password = Membership.GeneratePassword(8, 3) + "aaZZ09";
var newUserCreateResult = UserManager.Create(newUser, password);
if (newUserCreateResult.Succeeded == true) {
UserManager.AddToRole(newUser.Id, createUserDTO.SelectedRole);
string code = await UserManager.GeneratePasswordResetTokenAsync(newUser.Id);
code = HttpUtility.UrlEncode(code);
var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = newUser.Id, code = code }, protocol: Request.Url.Scheme);
await UserManager.SendEmailAsync(newUser.Id, "Reset Password", "Please confirm your account and reset your password by clicking <a href=\"" + callbackUrl + "\">here</a>");
return Redirect("~/Admin");
} else {
// Html.ValidationSummary displays these errors correctly
foreach (string msg in newUserCreateResult.Errors) {
ModelState.AddModelError(string.Empty, msg);
}
}
} catch (Exception ex) {
// Html.ValidationSummary DOESN NOT display these errors
ModelState.AddModelError(string.Empty, ex.Message);
}
if (newUser != null) {
UserManager.Delete(newUser);
}
createUserDTO.Roles = GetAllRolesAsSelectList();
return View("Create", createUserDTO);
}
我的Create.cshtml視圖:
@model Nissan.Models.CreateUserDTO
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
@using (Html.BeginForm("Create", "Admin", FormMethod.Post, new { @class = "form-horizontal", role = "form" })) {
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Create User</h4>
<hr />
@Html.ValidationSummary(false, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Email,
htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Email,
new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Email, "",
new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Roles,
htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.SelectedRole, (Model == null) ? null : Model.Roles);
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
@Html.ActionLink("Cancel",
"Index",
null,
new { @class = "btn btn-default" })
</div>
</div>
</div>
}
這個問題神秘地消失了(在重新啟動PC后?)。 現在,它應該按照異常發布錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.