1
我跟隨Shawn Wildermuth的MVC6優秀的Pluralsight教程,我一直在獲取一個驗證消息來顯示整個模型。模型驗證總結沒有顯示
這裏的行動:
[HttpPost]
public IActionResult Contact(ContactViewModel model)
{
if (model.Email.Contains("aol.com"))
ModelState.AddModelError("", "We don't support AOL addresses.");
if (ModelState.IsValid)
{
var toAddress = _config["MailSettings:ToAddress"];
_mailService.SendMail(toAddress, model.Email, "Contact Page", model.Message);
}
return View();
}
,當我提交表單,檢測到「美國在線」的地址,並添加到預期的ModelState
錯誤,然後IsValid
失敗,並返回View()
。
<h2>Contact Me</h2>
<form method="post">
<span asp-validation-summary="ModelOnly"></span>
<label asp-for="Name">Name</label>
<input asp-for="Name"/>
<span asp-validation-for="Name"></span>
<label asp-for="Email">Email</label>
<input asp-for="Email" type="email"/>
<span asp-validation-for="Email"></span>
<label asp-for="Message">Message</label>
<textarea asp-for="Message" cols="40" rows="4"></textarea>
<span asp-validation-for="Message"></span>
<div>
<input type="submit" value="Send Message"/>
</div>
</form>
不幸的是,DOM檢查顯示摘要的<span asp-validation-summary="ModelOnly"></span>
仍爲空。所有其他表單驗證按預期工作。
對此提出建議?
這奏效了 - 不知道爲什麼他沒有這個問題的視頻,但我很困惑,如何使用內聯級別的元素作爲容器。 –