2013-02-11 24 views
0

我試圖從我的視圖傳遞參數到我的控制器,我碰到: 值不能爲空當我試圖執行我的行動GetCompanyDataWithId。獲取值不能爲null與AJAX

成功的運行會將參數傳遞給控制器​​,該控制器將返回用於填充我的jqGrid的JSON數據。


看起來價值沒有被正確地傳遞 任何想法?

$("#dropdwnlist").change(function() { 
       var value = $(this).val(); 

       $.ajax({ 
        url: '<%= Url.Action("GetCompanyDataWithId", "Billing") %>', 
        type: "POST",        
        data: { companyid: "25" }, 
        success: function (data) { 
         alert(data); 
         $('#Bgrid').setGridParam({ url: '<%= Url.Action("GetCompanyDataWithId", "Billing") %>'}); 
         $('#Bgrid').trigger('reloadGrid');              
        }, 
        error: function(xhr, ajaxOptions, thrownError) { 
         alert(xhr.responseText); 
        } 

       }).done(function (msg) { 
        alert("Data Saved: " + msg); 
       }); 
      }); 
+0

Is'nt這真是一個serverside問題? – adeneo 2013-02-11 19:35:00

+0

迴應adeneo,它看起來像你有更多的技術,而不僅僅是jQuery。我認爲你使用的是某種版本的Asp.NET MVC和一些表格插件。如果你包含這些細節,它確實會幫助人們回答你的問題。至少,您可以捕獲您的更改函數發送的Ajax請求的主體並將其發佈到此處。 – 2013-02-11 19:37:30

回答

1

假設你的控制器的方法是這樣的:

public ActionResult GetCompanyDataWithId(string companyid) {} 

你可以使用這些$.ajax選項:

data: JSON.stringify({ companyid: "25" }), 
type: 'post', 
dataType: 'json', 
contentType: 'application/json; charset=utf-8', 

或者簡單:

Url.Action("GetCompanyDataWithId", "Billing", new { companyid = "25" }) 
+0

'JSON.stringify'可能不正確。控制器可能期望POST數據採用普通的'x-www-form-urlencoded'格式,而jQuery將自動執行該編碼。 – Barmar 2013-02-11 19:48:37

+0

@Barmar,這是必要的。尤其對於複雜的物體。 – Joe 2013-02-11 20:37:15