當我編輯產品時,客戶端驗證有效。我也想檢查服務器端驗證。我爲其他對象使用了以下代碼。有效。但是我在產品編輯頁面使用相同的代碼。這是行不通的:爲什麼我的請求只返回JSON,我的頁面丟失
jQuery(document).ready(function() {
...
if (jQuery('#ProductEditForm').validationEngine('validate')) {
jQuery.post('/Product/Edit', {
ProductId: jQuery('#ProductId').val(),
Price: jQuery('#Price').val(),
Name: jQuery('#Name').val(),
formname: 'Product_Edit_Form',
formtype: 'ProductEditF'
}, function (result) {
if (!result.success) {
alert(result.error);
}
});
return false;
} else {
return false;
}
...
});
在ProductController的:
[HttpPost]
public ActionResult Edit(Product model)
{
var result = //My Edit operation
if (result.IsSuccessfull)
{
return Json(new { error = "", success = true }, JsonRequestBehavior.AllowGet);
}
else
{
return Json(new { error = "Error occured!", success = false }, JsonRequestBehavior.AllowGet);
}
}
如果結果不是全成,動作返回僅此行:
爲什麼我的頁面輸?
,你到底在期待?順便說一句:你不需要允許得到,因爲JSON是從POST操作返回的。 – 2013-02-12 19:50:39
但是,[HttpPost] 公共ActionResult SignIn(UserLogin userlogin) {}的操作相同。但它的工作。我可以通過alert(result.error)從服務器獲取錯誤,並且我的頁面不會丟失。 – 2013-02-12 19:55:53
Edit方法期望使用'Product'類型的模型,但它沒有收到該模型。 – lopezbertoni 2013-02-12 20:28:45