怎么知道的ModelState包含错误 [英] How know if ModelState contains errors

查看:163
本文介绍了怎么知道的ModelState包含错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当一个形式发布在我的控制器,我做以下检查:

When a form is posted in my controller, I make the following check:

if(ModelState.IsValid)

如果模型无效,错误将被添加到的ModelState 。然后,该模型被传递到与验证摘要视图。

If the model is not valid, errors are added to the ModelState. The model is then passed to the view with validation summary.

不过,我想检查是否的ModelState 有从jQuery内就绪处理错误,所以我可以添加一些额外的行为,如果表单有错误。这可能吗?

However, I want to check if the ModelState has errors from inside the jQuery ready handler, so that I can add some additional behavior if the form has errors. Is that possible?

推荐答案

您可以吐全局JavaScript变量:

You could spit global javascript variable:

<script type="text/javascript">
    var isValid = @Html.Raw(Json.Encode(ViewData.ModelState.IsValid));
</script>

和则:

$(function() {
    if (!isValid) {
        alert('opa');
    }
});

这篇关于怎么知道的ModelState包含错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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