【发布时间】:2015-03-07 16:42:58
【问题描述】:
如果有人可以提供建议,我将不胜感激。
我有一个 Ajax 表单:
@using (Ajax.BeginForm("Edit", null, new AjaxOptions() {
UpdateTargetId = updateRegion,
InsertionMode = InsertionMode.Replace,
OnFailure = "formFailure" }))
{}
其UpdateTargetId 因当前用户角色而异:
@{
if (curUser.IsInputer)
{
updateRegion = "content";
}
else if (curUser.IsAuthorizer)
{
updateRegion = "mainPane";
}
}
如果模型状态无效,我希望始终返回 mainPane 中的视图:
<script>
function formFailure(result)
{
$("#mainPane").html(result.responseText);
}
</script>
但是当 ModelState 无效时不会调用 onFailure。为此,我在控制器中设置了错误代码:
public ActionResult Edit(ContractModel entity)
{
if(ModelState.IsValid)
{
if(curUser.isInputer) { return RedirectToAction("SomeAction");}
if(curUser.Authorizer) { return RedirectToAction("OtherAction");}
}
Response.StatusCode = 500;//internal server error to fire OnFailure of form
return PartialView(entity);
}
然后我得到了想要的结果,即我在mainPane div 中看到了带有错误的模型,在浏览器控制台中看到了内部服务器错误。但是,当我在本地运行应用程序时,它以这种方式工作,当我在服务器上发布并运行它时,我看到错误500 Internal server error,而不是部分视图。有什么解决方法吗?
编辑: 作为一个选项,我尝试在表单 OnSuccess 处理程序中检查模型是否有错误:
var isValid = @Html.Raw(Json.Encode(ViewData.ModelState.IsValid));
if (!isValid) {
$("#mainPane").html(result.responseText);
}
但我仍然可以在“内容”div 中看到视图。为什么会这样?
【问题讨论】:
标签: c# jquery asp.net asp.net-mvc asp.net-ajax