如何将JSON数组ajax / post到ASP MVC

时间:2013-02-19 21:47:03

标签: asp.net-mvc json jquery

尝试发布包含一些包含整数数组的JSON数据的帖子。按下我页面上的按钮来执行操作即可进入操作,但预期的数据不存在(两个int []变量为空)。在发布时执行网络配置文件显示请求正文包含以下数据:

  

组%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;
}

1 个答案:

答案 0 :(得分: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