1

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

如果结果不是全成,动作返回仅此行: enter image description here

为什么我的页面输?

+2

,你到底在期待?顺便说一句:你不需要允许得到,因为JSON是从POST操作返回的。 – 2013-02-12 19:50:39

+0

但是,[HttpPost] 公共ActionResult SignIn(UserLogin userlogin) {}的操作相同。但它的工作。我可以通过alert(result.error)从服务器获取错误,并且我的页面不会丢失。 – 2013-02-12 19:55:53

+0

Edit方法期望使用'Product'类型的模型,但它没有收到该模型。 – lopezbertoni 2013-02-12 20:28:45

回答

1

我想你想要的是这样的:

  jQuery.post('/Product/Edit', { 
       ProductId: jQuery('#ProductId').val(), 
       Price: jQuery('#Price').val(), 
       Name: jQuery('#Name').val(), 
       formname: 'Product_Edit_Form', 
       formtype: 'ProductEditF', 
       success: function(){ 
      alert('success'); 
     }, 
     error: function(){ 
     alert('failure'); 
     } 
      }); 
当然
+0

它仍然不起作用.. – 2013-02-12 20:05:23

+0

有趣。检查你的行动方法...你有检查它与断点? – 2013-02-12 20:08:34

+0

是的,我用断点检查它。结果返回false或true。对我来说这非常有趣,同样的结构在其他对象中也适用于我。我不会使用这个,会使用ViewBag发送错误来查看。谢谢 – 2013-02-13 06:00:07

相关问题