Html.ValidationSummary() vẫn đang được hiển thị ngay cả khi trạng thái mô hình hợp lệ.ASP.Net MVC: Chỉ hiển thị Html.ValidationSummary nếu có lỗi
Ví dụ này không hoạt động:
<% if (!this.ViewData.ModelState.IsValid)
{ %>
<%= Html.ValidationSummary()%>
<% } %>
Hiện vẫn còn là một sản phẩm nào 'ul' thẻ bị trả lại. Làm cách nào để làm cho nó hiển thị chỉ nếu ModelState không hợp lệ?
EDIT Tắt ModelState thực sự không hợp lệ, nhưng mã của tôi không thêm bất kỳ thông báo lỗi nào, nó không hợp lệ vì không có lý do rõ ràng.
[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();
}