开发者

Know if the form had validation errors in MVC 3

开发者 https://www.devze.com 2023-03-13 11:31 出处:网络
I have a form that I submit via AJAX. It is using the sta开发者_运维知识库ndard MVC validation. When the user first loads the page, this form is hidden. It only appears after the users click on a butt

I have a form that I submit via AJAX. It is using the sta开发者_运维知识库ndard MVC validation. When the user first loads the page, this form is hidden. It only appears after the users click on a button in the page.

I want to know if there is a way to know --in the view-- if there were errors in the validation. So, the next time the form is displayed, the form is being shown and not hidden.


You could know it like this:

if ($('#someFormId').valid()) {
    // the form is valid
} else {
    // the form contains validation errors
}

or if you wanted to test if there are some mode errors in the view:

@if (ViewData.ModelState.IsValid)
{
    // there are no errors to display
} else {
    // there were validation errors
}
0

精彩评论

暂无评论...
验证码 换一张
取 消