简体   繁体   English

MVC中的Ajax表单验证

[英]Ajax form validation in MVC

I have an Ajax form that I need to hit a JavaScript function on failure, like so: 我有一个Ajax表单,我需要在失败时点击JavaScript函数,如下所示:

using (Ajax.BeginForm("UpdateStages", new AjaxOptions
{
    HttpMethod = "POST",
    OnSuccess = "refreshSearchResults('" + @Model.First().ItemCode + "')",
    OnFailure = "showError"
}))

With the showError function taking the Ajax context response and appending it to a div , like so: 使用showError函数获取Ajax上下文响应并将其附加到div ,如下所示:

function showError(ajaxContext) 
{
    var response = ajaxContext.responseText;
    response = $($.trim(response));
    var itemVersion = response.filter("div")[0].innerHTML.trim().toString();
    var error = response.filter("p")[0].outerHTML.toString();
    $("#" + itemVersion.replace(".", "") + "-UpdateStagesResults").empty();
    $(error).appendTo("#" + itemVersion.replace(".", "") + "-UpdateStagesResults");
}

In order for the OnFailure to be called I have in my MVC controller ActionResult , when an error occurs: 为了在我的MVC控制器ActionResult调用OnFailure ,当发生错误时:

Response.StatusCode = (int)HttpStatusCode.BadRequest;
return PartialView();

which returns a PartialView with the error message in the ViewBag . 它返回一个PartialView ,其中包含ViewBag的错误消息。 This works fine when running locally, the error message is sent to the showError function and then the error is appended to the page. 这在本地运行时工作正常,错误消息被发送到showError函数,然后错误被附加到页面。

The problem is that when I put the application onto a live server (IIS7), the Ajax context is just 问题是当我将应用程序放到实时服务器(IIS7)上时,Ajax上下文就是这样

Bad Request 错误的请求

and for example is not: 例如不是:

<p>You have not entered a valid date.</p>
<div style="display:none;">V7.0  </div>

Any help would be great! 任何帮助都会很棒!

I've had this, 我有这个,

in IIS7 the default error settings are to show detailed error messages locally only., your view is replaced with the default for the status code. 在IIS7中,默认错误设置仅在本地显示详细的错误消息。您的视图将替换为状态代码的默认值。

If you want to see your custom errors, add this into your web.config 如果要查看自定义错误,请将其添加到web.config中

<system.webServer>
    <httpErrors errorMode="Detailed" />
</system.webServer>

that should sort it 应该排序它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM