简体   繁体   中英

2D Array angularjs to c#

I am able to call ac# method from an angular controllers as such:

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

My issues arises when i try to pass a 2D array from the angular to c# controller as such:

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

Any help would be appriciated.

Use the Newtonsoft Json library (must already be in your project).

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

send data using JSON.stringify

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

and C# function signature as

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

You should add contentType: "application/json; charset=UTF-8" into post header

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM