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

相關問題