2013-02-19 38 views
1

試圖做一個職位,包括一些JSON數據,其中有一個整數數組。按下我的頁面上的按鈕來執行該帖子即將進入該操作,但預期的數據不存在(兩個int []變量爲空)。做一個網絡配置文件而張貼示出了請求主體包括這樣的數據:如何ajax /發佈JSON數組到ASP MVC

組%5B%5D = 2個&組%5B%5D = 3個&警報%5B%5D = 5個&警報%5B% 5D = 9

的Javascript:

$('#modal-save').click(function() { 
       var selectedGroups = []; 
       var selectedAlerts = []; 
       $('input:checked').filter('[data-group="true"]').each(function() {selectedGroups.push($(this).data('id')); }); 
       $('input:checked').filter('[data-group="false"]').each(function() {selectedAlerts.push($(this).data('id')); }); 
$.ajax({ 
         type:'Post', 
         dataType: 'json', 
         url:'@Url.Action("UpdateAlertStores", new { alias = ViewBag.Alias})', 
         data: {groups: selectedGroups, alerts: selectedAlerts}, 
        }); 

MVC操作:

[HttpPost] 
public bool UpdateAlertStores(string alias, Guid? groupID, Guid? storeID, int[] groups, int[] alerts) 
{ 
    return true; 
} 

回答

2

添加traditional:true

traditional: true, 
type:'Post', 
dataType: 'json', 
url:'@Url.Action("UpdateAlertStores", new { alias = ViewBag.Alias})', 
data: {groups: selectedGroups, alerts: selectedAlerts}, 

此更改網址後看起來像:

groups=2&groups=3&alerts=5&alerts=9 
+1

完美,謝謝! – 2013-02-19 21:53:02

+1

很高興幫助... – 2013-02-19 21:54:59