jQuery检查ModelState是否有效 [英] Jquery to Check ModelState is Valid or not

查看:86
本文介绍了jQuery检查ModelState是否有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过jquery检查ModelState是否有效.在我的场景中,如果他单击提交"按钮,并且字段满足必需属性"的要求,我想打开模型弹出窗口,否则我们想显示错误,请问有人可以告诉我如何检查ModelState是否有效吗?/p>

I want to check whether the ModelState is Valid or not through jquery. In My scenario if he click on submit button, and the fields fulfilled the requirement of Required Attributes, i want to open model popover, otherwise we want to show the errors, can any one tell me how to check ModelState is valid or not?

推荐答案

为此,我认为您有两种解决方法.首先,您只需将ajax发布到您的控制器(例如:

For this I think you have two ways of working around. First you can simply make a ajax post to your controller (say:

public JsonResult IsModelStateValid(YourModel model)
{
  return Json(ModelState.IsValid);
}

,基于此您可以做任何您想做的事情.其次,如果您不想拨打服务器电话,只需检查

and based on this you can do whatever you want. Second, if you don't want to make a server call, you can simply check

var isValid = $('#frm').valid()

在您的jquery提交事件或按钮单击事件中.如果启用了非侵入式验证,并且对ASP.NET MVC的所有内置验证有效,而对自定义验证无效,那么这将起作用.

in your jquery submit event or button click event. This will work if you have enabled unobtrusive validation and works for all in built validations of ASP.NET MVC, but not for your custom validation.

这篇关于jQuery检查ModelState是否有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆