【问题标题】:2D Array angularjs to c#2D数组angularjs到c#
【发布时间】:2017-01-03 13:26:31
【问题描述】:

我可以像这样从角度控制器调用 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 数组从 angular 传递给 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";
}

如有任何帮助,我们将不胜感激。

【问题讨论】:

    标签: c# angularjs multidimensional-array


    【解决方案1】:

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

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

    【讨论】:

    • 我得到与其他答案相同的错误。当我尝试从 c# 返回 input[0][0] 时,我收到 NullReferenceException 错误,即使它已填充。有什么想法吗?
    • 调试模式下您的 2DArrayInCSharp 中有什么?对吗?
    【解决方案2】:

    使用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

    【讨论】:

    • 当我尝试从 c# 返回 input[0][0] 时收到 NullReferenceException 错误,即使它已填充。有什么想法吗?
    • 你从哪里得到这个异常?我的意思是那行代码
    猜你喜欢
    • 2020-09-07
    • 2017-10-25
    • 2014-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-06
    • 1970-01-01
    相关资源
    最近更新 更多