知道表单在MVC 3中是否存在验证错误

Car*_*nco 3 c# asp.net-mvc-3

我有一个表格,我通过AJAX提交.它使用标准的MVC验证.当用户首次加载页面时,此表单将被隐藏.它仅在用户单击页面中的按钮后显示.

我想知道是否有办法知道 - 在视图中 - 验证中是否有错误.因此,下次显示表单时,表单将显示而不是隐藏.

Dar*_*rov 7

你可以这样知道:

if ($('#someFormId').valid()) {
    // the form is valid
} else {
    // the form contains validation errors
}
Run Code Online (Sandbox Code Playgroud)

或者如果您想测试视图中是否存在某些模式错误:

@if (ViewData.ModelState.IsValid)
{
    // there are no errors to display
} else {
    // there were validation errors
}
Run Code Online (Sandbox Code Playgroud)