2017-09-13 54 views
1

當我在我的Angular應用程序中從我的MVC項目返回一個雙/二維數組時,它只返回一個數組。如何從我的MVC控制器返回一個雙數組到我的Angular應用程序?Angular,http返回double int數組

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); 
    } 

回答

0

JavaScript不知道這種2D數組,所以在反序列化後你不會得到預期的結果。相反,將你的模型聲明爲一個數組數組。喜歡的東西:

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

Json.Net通過正確序列化多維數組到數組的數組克服了這個問題:

JsonConvert.SerializeObject(model); 
+0

你真棒!謝謝! – NatJJ