2013-12-14 47 views
1

我正在使用mvc4應用程序。我使用.cshtml文件開發了一個表單,該文件繼承了模型並具有相應的控制器操作。 我使用的是AJAX的jQuery像提交表單,Ajax數據沒有得到控制器?爲什麼?

  var body=$('#formId').serialize(); 
      $.ajax({ 
       url: submitAction, 
       type: "POST", 
       datatype: "json", 
       data: body, 
       success: function (data) { 
       if (data != null) { 
       alert("success"); 
       } 
       }); 

「身體」是好的,它已序列化數據和submitAction是保存我的控制器操作和控制研究被轉移那裏的變種。

編輯:

我的控制器看起來像,

public JsonResult(ParentModel model) /*here model always hold null values, WHY??*/ 
    { 
    //stmts.. 
    return Json(new {success=true}, JsonRequestBehaviour.AllowGet); 
    } 

但是,有我的控制器操作的參數顯示空值。有人可以告訴可能是什麼錯誤,我該如何解決它?在你

回答

1
$.ajax({ 
    url: submitAction, 
    type: "POST", <-- you make post, but asp.net mvc controller receives default GET request 
    data: { model: body}, 

[HttpPost] 
public JsonResult(string model) //<--now you pass string and to Deserialize in ParentModel 
{ 
    JavaScriptSerializer jss= new JavaScriptSerializer(); 
    ParentModel pmodel = jss.Deserialize<ParentModel >(model); 

    return Json(new {success=true}, JsonRequestBehaviour.AllowGet); 
} 

嘗試編輯data:部分要求, 刪除數據類型: 「JSON」

和編輯類型model參數字符串

+0

對不起,它不作出任何差別。我很害怕;( – user2771399

+0

嘗試編輯數據:請求中的段落 –

+0

不要放棄))請參閱編輯 –

相關問題