Home > front end >  How to pass a 2d array of numbers to my .Net controller?
How to pass a 2d array of numbers to my .Net controller?

Time:03-25

I have a 2d javascript array like this

[[2,3],[13,4],[1,19]]

and I want to pass it to my .Net controller

My controller header looks like this

public async Task<ActionResult> UpdateOrder(int[,] order)

My put call looks like this

updateOrder(order: number[][]): Observable<any> {
   return this.http.put(this.baseUrl   'members/edit/set-order/'   order, {});
}

but I'm getting an error when I hit the controller saying:

'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Int32[,]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.\nTo fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.\nPath '', line 1, position 2.'

CodePudding user response:

I think you might need to use List<List<int>> type instead of int[,] in c# from default c# JSON serializer

public async Task<ActionResult> UpdateOrder(List<List<int>> order)

CodePudding user response:

You don't need to go through (manual) de/serialization and you don't need a List<List<int>>. I'm able to pass a 2d array of ints with a payload like [[1,2],[3,4],[5,6]] and the below API interface.

public async Task<IActionResult> Test(int [,] ints)

Isn't it your url in the request? You treat the order like a string to cat to the url and the body is empty. And when the body is empty, you see that error you're getting. I think what you need is:

return this.http.put('yourUrl', order, {});

See put. (assuming Angular)

  • Related