将Javascript JSON转换为.NET整数数组

时间:2015-09-08 08:04:40

标签: c# json

我试图将json数据传递给.net mvc控制器。似乎mvc自动将id和lastdatetime转换为正确的格式,但不是int [] currentIds,任何人都知道为什么?

var jsonData = {
                "id": id,
                "lastDataTime": lastDateTime,
                "currentIds": [1, 2, 3, 4]
            };


public void Process(int id, DateTime lastDateTime, int[] currentIds)
{

}

2 个答案:

答案 0 :(得分:2)

请试试这个:

$.ajax({
            type: "POST",
            url:"@Url.Action("Index", "Home")" ,
            data: {
                "id": id,
                "lastDataTime": lastDateTime,
                "currentIds": [1, 2, 3, 4]
            },
           dataType: "json",
           traditional: true,
           success: function(msg){
                       alert(msg)
                   }
    });


public ActionResult Index(int id, DateTime lastDateTime, int[] currentIds)
{

}

答案 1 :(得分:1)

简化此问题并让阵列先运行,试试这个:

查看

var myArray = ["1", "2", "3","4"];


$.ajax(
                {
                    type: "POST", 
                    url: '/ControllerName/ActionMethod/',
                    data: JSON.stringify(myArray),
                    cache: false,
                    //dataType: "html",
                    success: ///            
})

<强>控制器

public ActionResult Index(List<String> currentIds)
{
   ///
}

调试此项并检查列表是否已填充,然后介绍其他对象。