Html.ValidationSummary() is still being rendered even if the model state is valid.
This example doesn't work:
<% if (!this.ViewData.ModelState.IsValid)
{ %>
<%= Html.ValidationSummary()%>
<% } %>
There is still an empty 'ul' tag being rendered. How do I make it render only if the ModelState is not valid?
EDIT Turns out the ModelState is really invalid, but my code does not add any error messages, it's just invalid for no obvious reason.
[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();
}
If the info you provide is correct, then this.ViewData.ModelState.IsValid
is most definitely false. There must be other code here which you don't provide.
The source code says that when the model state is valid, the helper returns a null string. I suspect that your model state is really invalid but that there hasn't been a message added. Or, it could be that the markup is really coming from something else on your page -- maybe even added with javascript.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With