簡體   English   中英

2D數組angularjs到c#

[英]2D Array angularjs to c#

我可以從這樣的角度控制器中調用ac#方法:

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

任何幫助將被申請。

使用Newtonsoft Json庫(必須已經在您的項目中)。

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

使用JSON.stringify發送數據

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

和C#函數簽名為

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

您應該在post header添加contentType: "application/json; charset=UTF-8"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM