Sending an array of JavaScript objects to an MVC controller using POST method

After constructing an array of JavaScript objects using jQuery across multiple table rows, the structure looks like this:

obj_rows = [
    {"param1":value11, "param2":value12, "param3":value3},
    {"param1":value21, "param2":value22, "param3":value23},
    //more objects with the same parameters
]

I am looking for a way to send an AJAX POST request to a Controller method named SomeAction in SomeController that can handle this array and iterate over all objects. I have tried using JSON without success.

It is important to note that even though MVC is being used, there is no corresponding model class for these objects. Ideally, I would like to end up with a structure such as a List of 3 Tuples.

Answer №1

One effective solution is to convert the object into a string using stringify before sending it through the ajax request.

Simply use: JSON.stringify(obj_rows) and on the receiving endpoint, you will receive an array (which has been stringified). Simply parse it back and you will have your array ready to use.

Answer №2

One way you can accomplish this task is by utilizing the JSON.stringify() method in JavaScript to convert your object into a string. You can then store this string in a hidden field within your view. Upon receiving this data in your controller, you have the option to either deserialize it back into an object or manually parse it as needed.

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

JSDOM failing to retrieve complete list of elements from webpage

I'm currently struggling with a simple webscraper using JSDOM. Here's the code snippet I've been working on: const axios = require("axios"); const jsdom = require("jsdom"); const { JSDOM } = jsdom; let v = "15" ...

Display the size of the data array in VueJS once the data has been retrieved from the API

Using Vue JS, I have been attempting to retrieve data from an API. My goal is to determine the length of the array and then log it to the console using a method. However, every time I try to do this, the value logged is always "0" instead of the actual le ...

Display the ViewModel in the view once the ajax call has been made

When I make an AJAX call to invoke an action, pass my ViewModel to it, and then attempt to open a new view with a different ViewModel, the redirectToAction and return view methods are not functioning as expected. Other solutions I have come across only inv ...

Is React the best solution for managing a lengthy list that requires constant data updates?

In order to display a long list with over 2000 entries that changes dynamically, I am utilizing react redux. Each second, at least one new row is added to the list data. My current approach involves mapping through the list data in the render method like t ...

Refresh only a portion of a page using PHP and JavaScript

I have a webpage set up with multiple sections in separate divs. Currently, the essential divs required are <div id="main">...</div> & <div id="sidebar">...</div> Each div contains PHP code like: <?php include("page.php") ...

Unable to update angular $scope using ng-click

I am attempting to modify a directive controller's $scope using ng-click. There is a simple button in my code: <button ng-click="showEnterKeyField = !showEnterKeyField">btn {{showEnterKeyField}}</button> This code functions as expected, ...

Refresh the table following deletion of a row from firebase

I have developed an application where I display data from a firebase object in a table. However, I have encountered an issue where I need to manually refresh the page every time I remove a row from the table. Is there a way to automate this process? The sa ...

Tally the values entered into the text input field

I am interested in counting the number of IDs within an input of type "text" The values return like this: 1, 4, 6, etc. <input type="hidden" class="selected_ids" value="selected_ids" name="selected_ids[]" multiple="yes" id="selected_ids" /> ...

The PHP script retrieves the entire content of the file

Currently in the process of learning PHP, I am experimenting with connecting PHP and JavaScript using AJAX calls. Here is how my JavaScript file is set up: $.ajax({ type:"GET", url:"test.php", success:function(data) { console.log(data) ...

converts json data into a newtsoft object

I am currently facing an issue with deserializing my JSON data. The structure of the JSON is as follows: { "cart.empty.title":"Il tuo carrello \u00e8 vuoto", "returns.confirmation.status.step1.next.arbitration":&quo ...

When changes are discarded, the checkbox will remain in its current state

I'm struggling with a small issue that's causing headaches. In my modal, I display some information with checkboxes. The data comes from an array where the checkbox states are set based on the initial values in the array. Here is an example of t ...

What is the method for connecting event listeners to an HTML element right when the element becomes visible on the webpage?

I prefer not to delay until the entire HTML DOM is fully loaded. It's important to clarify that I'm specifically referring to elements that are present in the HTML code but have not been rendered yet, rather than dynamically loaded elements. ...

Revamping the Look: Refreshing Background of Div

I'm attempting to change the background image of the body element on a webpage when I hover over links with data-* attributes. It's working perfectly, but I can't seem to figure out how to create a smooth fade between the images when a link ...

Having trouble retrieving JSON data from the WordPress API using iOS Swift?

I'm attempting to retrieve a JSON file from a local Wordpress server and display it using SwiftyJson and Alamofire. Below is the code snippet I am using: Alamofire.request(.GET, "http://localhost:8080/wordpress/feed/json").response { (req, res, data, ...

What are the steps for setting up a dual-phase MailChimp registration form?

I'm looking for guidance on utilizing the MailChimp API to implement a two-step signup process. Despite my efforts, I've been unable to locate any relevant documentation on this specific topic. The desired sequence is as follows: Step 1: The in ...

Unable to navigate beyond the boundaries of an app

I am currently facing an issue with my authentication service where it redirects to the identity server in case of certain errors. I attempted to achieve this using window.location.href = environment.authCodeFlowIssuer;, however, an error occurred: Ref ...

I mistakenly downloaded the incorrect NPM package. Do I need to be concerned?

After running a command in CMD to install gulp, I mistakenly used the wrong command: npm install glup I'm concerned that this may have installed some harmful package and potentially harm my PC. As someone new to using npm, I am quite apprehensive ab ...

What is the best way to incorporate JSON into Sqlite when working with Rails?

I want to extract data from my rails sqlite database in JSON format, but I'm not sure how to go about it. I've already set up all the basics. Can you suggest any gems and explain how I can integrate them into an existing project that uses sqlite? ...

Converting Plain JSON Objects into a Hierarchical Folder Structure using Logic

Looking at the data provided below: [ {name: 'SubFolder1', parent: 'Folder1'}, {name: 'SubFolder2', parent: 'SubFolder1'}, {name: 'SubFolder3', parent: 'SubFolder2'}, {name: 'Document ...

Error 404: Page not found. Sorry, we couldn't locate the Node JS and Express resource

I have been trying to access the routes /api and /api/superheroes, but I keep encountering an error message whenever I try. Not Found 404 NotFoundError: Not Found at C:\Users\mikae\Desktop\Project\node-express-swig-mongo\a ...