问题
I am working on an MVC 4 project that is constantly showing the ValidationMessage even on a simple GET method that does no validation or model binding of complex types:
//location/{locationId}/announcement
[HttpGet]
public ActionResult LocationMakeAnnouncement(int locationId)
{
var pageViewModel = this.BuildLocationMakeAnnouncementViewModel(locationId);
return View(pageViewModel);
}
The BuildLocationMakeAnnouncementViewModel
only builds up the ViewModel and doesn't touch the ModelState.
Then on the view I have:
<span class="errorArea">@Html.ValidationMessage("ProductText", " ", new { @class = "inputErrorIcon" })</span>
Which emits:
<span class="errorArea"><span class="field-validation-valid inputErrorIcon" data-valmsg-for="ProductText" data-valmsg-replace="false"> </span></span>
Outputting the ModelState shows that it does not have any errors
@ViewData.ModelState.Values.Any(x => x.Errors.Count >= 1)
Why would ValidatioMessage output the when there are no errors?
ANY suggestions?
回答1:
If you want to keep the client validation enabled, I had a similar issue with ValidationSummary
, but it's just the way these things render, I believe. All you need to do is set the style for the "valid" stuff to display: none
. In your case, that seems to be:
.field-validation-valid { display: none; }
回答2:
Turns out this is the behavior if you enable ClientValidationEnabled and UnobtrusiveJavaScriptEnabled.
So setting them to false fixed my problem:
<add key="ClientValidationEnabled" value="false" />
<add key="UnobtrusiveJavaScriptEnabled" value="false" />
来源:https://stackoverflow.com/questions/13151187/validationmessage-is-showing-on-initial-get-method