What is the best way to send a 2D array of numbers to my .Net controller?

My JavaScript array is structured like this:

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

I need to send it to my .Net controller.

The header of my controller function is as follows:

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

This is how I make the PUT call:

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

Unfortunately, when I try to access the controller, I encounter an error message:

'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.'

Answer №1

In my opinion, it would be beneficial to utilize the List<List<int>> type in place of int[,] when working with JSON serialization in C#.

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

Answer №2

No manual de/serialization is required, nor do you require a List<List<int>>. A 2-dimensional array of ints can be passed with a payload such as [[1,2],[3,4],[5,6]] using the following API interface.

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

Make sure to include your URL in the request. Treat the order like a string that concatenates to the URL, and ensure the body is not empty. The error may occur if the body is left empty. Try:

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

Refer to put. (assuming Angular)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to remove a portion of a string?

Looking to remove a section of a string using JavaScript? I attempted var sentence = "C:\mnt\c\User\Foo\Bar"; var updatedSentence = sentence.replace("mnt\c", ""); But the result was not as expected ...

Retrieve an item from a MongoDB database using Mongoose

It seems like a simple problem, but my brain is struggling to comprehend the issue at 2 AM. I'm working on creating a profile page that displays basic public information. My approach involves retrieving the user's username from MongoDB based on t ...

Utilizing Compact Arrays for Efficient Data Retrieval

Dealing with a large image array, I've compressed it for efficiency. To retrieve the pixels in the array, I've devised two methods. def getPixels(a) data = a.unpack('S9s') end def setPixel(array, indexArray, value) index = indexArr ...

How can I customize the color of grayed-out days in Fullcalendar's validRange?

Hello there, I am currently using fullcalendar version 5.10.1 for my project and I am looking to change the color of already passed days to a more grayish tone as shown in the picture. Unfortunately, I am unable to utilize Ajax and can only use CSS styles. ...

Error Message: "Oops! It looks like you are trying to access a property or method

I am in the process of developing a static website that includes tabs (a reactive property), each containing multiple cards (also a reactive property) with images and text. Recently, I encountered the following warning: [Vue warn]: Property or method &qu ...

Prevent unnecessary clicks with Vue.js

In my vue.js application, there is a feature to remove items. The following code snippet shows the div element: <div class="ride-delete" @click="delete"> <p>Delete</p> </div> This is the function used to handle the click ...

Show the array in the input field as an array

Check out my Plunker demo at the following link: https://plnkr.co/edit/stKf1C5UnCKSbMp1Tyne?p=preview angular.module('listExample', []) .controller('ExampleController', ['$scope', function($scope) { $scope.names = [&ap ...

Generating real-time video from jpeg images with Gstreamer and JavaScript's setTimeout() function

In my attempt to set up a live "video" stream, I am using an HTML tag on a web page. A continuous Gstreamer pipeline updates a file titled "snapshot.jpeg" with new frames captured from a webcam through video4linux2 at a frame rate of 15 fps. The webpage ...

Utilize forge-node for encryption and bouncyCastle for decryption with Node.js

As part of my encryption process, I first generate a key pair and then encrypt the private key using a passphrase. Below is an example of this process implemented in Node.js with the node-forge library: const keypair = forge.pki.rsa.generateKeyPair({ bits: ...

Ways to troubleshoot the issue of a non-working "Onclick function"

let username; document.getElementById("mySubmit").onclick= function(){ username= document.getElementById("myText").value; document.getElementById("myH1").textContent= `Hello ${username}` } <!DOCTYPE html> <html lang="en"> <head> < ...

Obtain the remaining portion of the array once a match has been identified

I'm currently working on a regex and it is functioning properly. However, my goal now is to extract the results. Essentially, I am looking to retrieve the contents of the title meta tag if the name/property/etc contains "title". To clarify, I want t ...

Execute a select query based on the chosen date from the CalendarExtender

Is there a way to execute a select query based on the selectedDate value of CalendarExtender in an ASP.NET application? There is a hidden dummy button that triggers a button click event on the Calendar Extendar using OnClientDateSelectionChanged="checkD ...

What is the best way to position a div in the center of a page using Jquery?

I am trying to center my div on the page using the following CSS code: .content{ width:1000px; height:600px; margin:auto; margin-top:auto; } What I Want to Do : I would like to achieve the same effect using jQuery within $(document).ready(functi ...

Parsing iTunes XML files using C#

Trying to parse iTunes XML data: <feed xmlns:im="http://itunes.apple.com/rss" xmlns="http://www.w3.org/2005/Atom" xml:lang="en"> <id>http://itunes.apple.com/us/rss/topSongs/limit=100/genre=24/xml</id> <title>iTunes Store: ...

Tips for implementing filters in Angular2 without using the package field in the console

I am currently experiencing an issue with a filter field in my code. The filter works fine when all the package data is present, however, some items do not have a package field. As a result, I need to filter based on the package name but I am encountering ...

Access the values stored in a Key/Value variable within a JavaScript environment

Let's dive into this situation: Imagine I have a Key/Value pair like this: var dict = {"78": "X", "12": "G", "18": "R", "67": "U", "68": "O", "30": "P"} Now, suppose I have a string and I ...

Having trouble reaching the JSON data, resorting to utilizing variables instead

I attempted to retrieve information regarding the status of certain buttons in JSON format using the $.getJson() method. The ID with a dash has been split into two parts and stored in an array regarr. However, I am unable to retrieve the data from JSON usi ...

Retrieving the body of a GET request using NodeJS and axios

Let me share my request with you: axios.get(BASE_URI + '/birds/random', {Stuff: "STUFF"}) .then(randBird=>{ const birdData = randBird.data const bird = { age: birdData.age, ...

Over-extended Affix in Bootstrap 3.1.0

I'm currently using Bootstrap 3.1.0. The issue I've encountered is that when the "affix" becomes too long for the viewport, it ends up getting cut off and doesn't display the bottom items. Is there a way to make Bootstrap's affix featu ...

What is the process for getting involved with npm commands?

Here is an excerpt from my package.json file: "scripts": { "cpFile": cp ../template/index.js /src/view/home/ } When I try to run the command: npm run cpFile fileName.js I expect it to execute: cp ../template/index.js /src/view/home/fileName.js How ...