将JSON数据作为数组传递给MVC控制器属性(模型绑定)

时间:2016-10-03 13:44:00

标签: jquery json asp.net-mvc vb.net

我正在尝试使用jQuery Ajax传递整数数组,但我的控制器似乎没有接收数据。

jQuery AJAX调用:

var box = {MECallButtons: R_MainEntranceButtons }

$.ajax({
    type: "POST",
    url: "/Home/CollectJson/",
    contentType:"application/json",
    data: JSON.stringify(box),
    success: function (data) {
        alert(data);
    }
});

数据模型:

Public Class MyParamModel
    Private f_MECallButtons(100) As Integer
    Public Property MECallButtons(ByVal Index As Integer) As Integer
        Get
            Return f_MECallButtons(Index)
        End Get
        Set(ByVal value As Integer)
            f_MECallButtons(Index) = value
        End Set
    End Property
End Class

控制器:

Function CollectJson(ByVal test As MyParamModel) As JsonResult
    Return Json("12345")
End Function

Mozilla控制台(发送Json数据):

{"MECallButtons":[4,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
,0,0,0,0,0,0,0,0,0]}

在visual studio中,属性数组中的所有元素都是0。我似乎没有收到第一个2元素4 & 1

任何帮助都将不胜感激。

由于

1 个答案:

答案 0 :(得分:0)

删除Stringify并将对象分配给post应该有效。

var box = {MECallButtons: R_MainEntranceButtons }

$.ajax({
    type: "POST",
    url: "/Home/CollectJson/",
    contentType:"application/json",
    data: {model: box},
    success: function (data) {
        alert(data);
    }
});

然后将控制器中的参数名称设置为

Function CollectJson(ByVal model As MyParamModel) As JsonResult
    Return Json("12345")
End Function