当我编辑产品时,客户端验证有效。我也想检查服务器端验证。我为其他对象使用了以下代码。有效。但是我在产品编辑页面使用相同的代码。这是行不通的:为什么我的请求只返回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