假设你有下面的控制器操作如何在ajax调用中处理不同的结果?
[HttpPost]
public ActionResult Save(CustomerModel model)
{
if (!ModelState.IsValid) {
//Invalid - redisplay form with errors
return PartialView("Customer", model);
}
try {
//
// ...code to save the customer here...
//
return PartialView("ActionCompleted");
}
catch (Exception ex) {
ActionErrorModel aem = new ActionErrorModel() {
Message = ex.Message
};
return PartialView("ActionError", aem);
}
}
而且假设你使用jQuery调用这个动作:
$.ajax({
type: "post",
dataType: "html",
url: "/Customer/Save",
sync: true,
data: $("#customerForm").serialize(),
success: function(response) {
/*
??????
*/
},
error: function(response) {
}
});
我希望能够我正在处理他们的成果来区分客户端上的不同方式。换句话说,我怎么能明白,行动
- 返回相同的模型,因为还没有通过验证
- 返回的表示错误信息的意见/邮件
任何建议吗?
+1 - 正在输入这个,可能是最好的方法。 – 2010-10-02 10:36:47
非常好!我无法想象一个更清洁的方式!无论如何,这使我改变了每一个行动。我猜如果这可以在ActionFilter属性中以自动方式与尼克答案混合完成...... P.S. :)是的,它是'async:'false'' – Lorenzo 2010-10-02 10:46:00