jQuery的验证,ASP.NET MVC的ModelState错误(异步POST) [英] jQuery Validate, ASP.NET MVC ModelState Errors (Async POST)

查看:146
本文介绍了jQuery的验证,ASP.NET MVC的ModelState错误(异步POST)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发展与asp.net MVC 3 web应用程序,我有某种形式的POST到一个异步操作(通过AJAX)。这一行动收到
一些数据注释的视图模型来验证它。验证工作正常,但当验证器返回一个错误,我不知道我怎么能返回它在我的视图来显示(因为POST被阿贾克斯制造)。

I'm developing a web app with asp.net mvc 3 and I have some form that POST to an async action (via ajax). This action recives a ViewModel with some data annotations to validate it. The validation works fine but when the validators return a error I don't know how can I return it to show in my view (because the POST was made by ajax).

我的动作是一样的东西:

My action is something like:

[HttpPost]
public ActionResult SaveCustomer(CustomerViewModel input) {
    if (!ModelState.IsValid) { // <-- business validation
        return Json(new { success = false, errors = ???});
    }
    // persist 
    return Json(new { success = true });
}

我怎样才能显示在我看来,这样的错误与JQuery验证?
如果这是可以张贴一些code来样...我会AP pretiate了!

How can I show this errors with jquery validate in my view? If it's possible to post some code to sample... I would appretiate that!

谢谢你们!

推荐答案

而不是在错误的情况下发送JSON我把表单内的部分,然后让控制器动作返回这部分的错误的情况。使用JSON的问题是,你实际上可以使用LINQ获取来自的ModelState错误,但它可能是一个PITA展示他们的看法。

Instead of sending JSON in case of error I would put the form inside a partial and then have the controller action return this partial in case of error. The problem with JSON is that you could in fact fetch the errors from the ModelState using LINQ, but it could be a PITA to show them on the view.

所以:

<div id="myform">
    @Html.Partial("_MyForm")
</div>

,然后在 _MyForm.cshtml

@model CustomerViewModel
@using (Html.BeginForm())
{
    @Html.EditorFor(x => x.Foo)
    @Html.ValidationMessageFor(x => x.Foo)
    <br />
    @Html.EditorFor(x => x.Bar)
    @Html.ValidationMessageFor(x => x.Bar)
    <br />
    <input type="submit" value="OK" />
}

和控制器动作将成为:

[HttpPost]
public ActionResult SaveCustomer(CustomerViewModel model)
{
    if (!ModelState.IsValid)
    {
        return PartialView("_MyForm", model);
    }
    return Json(new { success = true });
}

和最后一步是AJAXify这种形式可以在一个单独的JavaScript文件来完成:

and the last step is to AJAXify this form which could be done in a separate javascript file:

$(function () {
    $('#myform').delegate('form', 'submit', function () {
        $.ajax({
            url: this.action,
            type: this.method,
            data: $(this).serialize(),
            success: function (result) {
                if (result.success) { 
                    // We have a JSON object in case of success
                    alert('success');
                } else {
                    // We have the partial with errors in case of failure
                    // so all we have to do is update the DOM
                    $('#myform').html(result);
                }
            }
        });
        return false;
    });
});

这篇关于jQuery的验证,ASP.NET MVC的ModelState错误(异步POST)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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