ValidationMessage is showing on initial GET method

喜欢而已 提交于 2019-12-25 11:34:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!