即使模型状态有效,Html.ValidationSummary()仍然呈现。ASP.Net MVC:仅当出现错误时才呈现Html.ValidationSummary
这个例子不起作用:
<% if (!this.ViewData.ModelState.IsValid)
{ %>
<%= Html.ValidationSummary()%>
<% } %>
还有所呈现空 'UL' 标签。 如果模型状态无效,我如何才能使其仅显示?
编辑 原来的ModelState中确实是无效的,但我的代码中不添加任何错误消息,这是没有明显的理由只是无效。
[AcceptVerbs("POST")]
public ActionResult Login(string username, string password, bool? remember)
{
if (string.IsNullOrEmpty(username))
{
ModelState.AddModelError("Username", "Username is required");
}
if (string.IsNullOrEmpty(password))
{
ModelState.AddModelError("Password", "Password is required");
}
if (ModelState.IsValid)
{
; // this point is never reached
}
return View();
}