假設你有下面的控制器操作如何在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