Angular,http返回双int数组

时间:2017-09-13 06:29:40

标签: angularjs asp.net-mvc

当我在我的Angular应用程序中从我的MVC项目返回一个双/二维数组时,它只返回一个数组。如何从我的MVC控制器返回一个双数组到我的Angular应用程序?

   function getStatusView() {

    dataFactory.getStatusView()
        .then(function (response) {
            $scope.statusview = response.data.StatusViewList;
            $scope.modelList = response.data.ModelList;
            $scope.modelClustersList = response.data.ModelCLustersList;
            $scope.turbineNumberDistinct = response.data.TurbineNumbersDistinct;
            $scope.alarmArray = response.data.statusArray;

            }, function (error) {
            $scope.status = 'Unable to load customer data: ' + error.message;
        });
}



   [HttpGet]
    public JsonResult GetStatusView()
    {
        model.statusArray = new int[model.ModelList.Count, model.StatusViewList.Count];

        var statuslinesWithAlarm = statusView.Where(p => p.AlarmLevel> 0).ToList();

        foreach (var statuslineWithAlarm in statuslinesWithAlarm)
        {
            var turbineIndex = model.StatusViewList.Select(p => p.TurbineNumber).ToList().IndexOf(statuslineWithAlarm.TurbineNumber);
            var modelIndex = model.ModelList.IndexOf(statuslineWithAlarm.ModelName);

            model.statusArray[modelIndex,turbineIndex] = 1;

        }
        return Json(model, JsonRequestBehavior.AllowGet);
    }

1 个答案:

答案 0 :(得分:0)

Javascript不知道这种2D数组,因此在反序列化后你不会得到预期的结果。而是将您的模型声明为数组数组。类似的东西:

var model = new int[][] { new [] { ... }, new [] { ... }, new [] { ... } }

Json.Net通过将多个维度数组正确序列化为数组数组来克服此问题:

JsonConvert.SerializeObject(model);