2017-01-03 122 views
0

我能够从一个角度控制器调用C#方法,例如:二维数组angularjs到C#

angualarjs:

 $http.post('myC#Function', { "input": input) 
     .success(function (response) { 
      console.log("success send response: " + response); 
     }) 
     .error(function (response) { 
      console("error send response: " + response); 
     }); 

C#

  public string myC#Function(string input) 
    { 
     return "success"; 
    } 

我的问题出现时,我尝试从角度传递2D阵列到C#控制器,例如:

angularjs:

 var my2DArray= [[], []]; 
    // array is populated here (not shown) 
     $http.post('myC#Function', { "inputArray": my2DArray) 
     .success(function (response) { 
      console.log("success send response: " + response); 
     }) 
     .error(function (response) { 
      console("error send response: " + response); 
     }); 

C#

 public string myC#Function(string[,]input) 
{ 
    return "success"; 
} 

任何帮助将appriciated。

回答

0

使用Newtonsoft Json库(必须已经存在于您的项目中)。

public string YourCSharpFunction(string yourJson2DArrayFromAngular) 
{ 
    var your2DArrayInCSharp = JsonConvert.DeserializeObject<string[,]>(yourJson2DArrayFromAngular); 
    return "success"; 
} 
+0

即时得到相同的错误对方的回答。当我尝试从c#返回输入[0] [0]时,即使填充它,我也会收到NullReferenceException错误。有任何想法吗? – sgov090

+0

你在调试模式下的your2DArrayInCSharp中有什么?这是对的吗 ? – Floc

0

使用JSON.stringify

$http.post('myC#Function', { JSON.stringify({ input : input }) }) 

发送数据和C#函数签名

public string myC#Function(string[][] input) {} 

您应该添加contentType: "application/json; charset=UTF-8"post header

+0

当我尝试从c#返回输入[0] [0]时,即使填充它,我也收到NullReferenceException错误。有任何想法吗? – sgov090

+0

你在哪里得到这个异常?我的意思是这行代码 –