Html.ValidationSummary(false, “message”) is always showing, even on page load

前端 未结 3 1098
没有蜡笔的小新
没有蜡笔的小新 2020-12-05 06:34

I am using client side validation and I would like the message below to show only when I have an error. I am trying to use this a general error in case any field is invalid.

相关标签:
3条回答
  • 2020-12-05 06:55

    I think the issue is the fact the Html.ValidationSummary has to appear before the Html.BeginForm otherwise the message is always displayed.

    0 讨论(0)
  • 2020-12-05 07:03

    If you use a developer tool in your browser to inspect the validation summary text you'll see that it has the class validation-summary-valid when it is clear but validation-summary-errors when there are form errors.

    Therefore, just create a css rule as follows;

    .validation-summary-valid {
        display:none;
    }
    

    and all should be good.

    0 讨论(0)
  • 2020-12-05 07:13

    Initially I was checking for a List property on page load so I thought of passing a new model. Then the validation summary just appeared. When I changed my code from

    return View(new myModel)
    

    to

    return View()
    

    the validation summary did not appear on Get. I also added a null check on the model when checking the property so I can use the latter code.

    0 讨论(0)
提交回复
热议问题