ASP.NET MVC" Ajax.BeginForm"执行的onSuccess即使模式是无效的 [英] ASP.NET MVC "Ajax.BeginForm" executes OnSuccess even though model is not valid

查看:446
本文介绍了ASP.NET MVC" Ajax.BeginForm"执行的onSuccess即使模式是无效的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个提交反馈的形式,它使用Ajax.BeginForm渲染部分包含表单元素。所述的onSuccess事件被触发,即使ModelState中是无效的。这正常吗?我期待能够做导致一个无效的模型几回传,那么当该模型是有效的,没有错误那么的onSuccess事件将触发?

I have a "submit feedback" form which uses "Ajax.BeginForm" to render a partial containing the form elements. The OnSuccess event is triggering even if the ModelState is not valid. Is this normal? I was expecting to be able to do a few postbacks resulting in an invalid model, then when the model is valid and there are no errors then the OnSuccess event would trigger?

推荐答案

我处理这个问题有一个相当简单的JavaScript技术:

I handle this issue with a fairly simple javascript technique:

首先设置你的的onSuccess 是这样的:

First setup your OnSuccess like this:

OnSuccess = "UpdateSuccessful(data)"

那么你这样的JavaScript函数:

Then your javascript function like this:

function UpdateSuccessful(data) {
    if (data.indexOf("field-validation-error") > -1) return;

    // Do your valid stuff here
}

这样的话,就没有必要惹控制器,或者更重要的是,你的控制器可以返回部分海景与模型错误而不做任何奇怪的,即

This way, there is no need to mess with your controller, or more importantly, your controller can return the Partial View with the model errors without doing anything weird, ie:

    public ActionResult SaveDetails(Project model)
    {
        if (ModelState.IsValid)
        {
            model.SaveProject();
        }

        return PartialView("ProjectForm", model);
    }

而在你的 AjaxOptions

UpdateTargetId = "FormContents"

现在只要确保你有一个 DIV 或一些与 ID =FormContents无论你想你的表单显示。

Now just make sure you have a div or something with id="FormContents" wherever you want your form displayed.

这篇关于ASP.NET MVC" Ajax.BeginForm"执行的onSuccess即使模式是无效的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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