Passing JavaScript Array to MVC Controller

I'm facing an issue with my JavaScript array when trying to send it to a controller.

function SubmitData()
{
    var dataCollection = new Array();

    var test1 = { name: "Alice", age: "30", address: "123 Main St" };
    dataCollection.push(test1);
    var test2 = { name: "Bob", age: "40", address: "456 Elm St" };
    dataCollection.push(test2);
    var test3 = { name: "Charlie", age: "50", address: "789 Oak St" };
    dataCollection.push(test3);

    var jsonDataToPost = JSON.stringify(dataCollection);

    $.ajax({
        type: "POST",
        url: "Home/SaveEntry",
        datatype: JSON,
        data: { inputData: jsonDataToPost },
        traditional: true
    });
}

The C# controller includes the following class:

public class EntryDetails
{
    public string name { get; set; }
    public string age { get; set; }
    public string address { get; set; }
}

and this method:

public void SaveEntry(List<EntryDetails> inputData)
{
    foreach (EntryDetails item in inputData)
    {
      string details =  item.name + item.age + item.address;
    }
}

Even though I have set traditional: true, when debugging, the value passed to the controller method is always NULL or 0. The array does not seem to be passing correctly. Any suggestions on how to resolve this issue?

Answer №1

To enhance your method, consider including the following signature:

[HttpPost]
public void AddPerson(List<newEntry> methodParam)

Additionally, follow Gavin's suggestion to utilize data: dataToPost

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

Foundation tabs malfunction within a modal window when implementing ajax functionality

I utilized the basic tabs HTML found at http://foundation.zurb.com/docs/components/tabs.html and implemented it as an Ajax response. Within my index.html file, I included the following: <a href="ajax/endpoint" data-reveal-ajax="true" class="but ...

Steps for creating a link click animation with code are as follows:

How can I create a link click animation that triggers when the page is loaded? (function () { var index = 0; var boxes = $('.box1, .box2, .box3, .box4, .box5, .box6'); function start() { boxes.eq(index).addClass('animat ...

Having trouble sending AJAX select options with Choices.js

I'm currently utilizing Choices.js (https://github.com/jshjohnson/Choices) along with Ajax to dynamically populate data into my select field from a database. However, I've encountered an issue when using the following code: const choices = new C ...

RadScheduler refresh rate

Currently I am incorporating RadScheduler into my project. The scheduler requires a regular update, so I have implemented a JavaScript function with an interval to call rebind() on the RadScheduler every 60 seconds. However, an issue arises when the user o ...

What is the best way to activate a function after all the AJAX calls within a function have finished executing?

My challenge lies in determining when all the AJAX calls have completed. Below is the function I am working with. function getValuesForTablePropertySelected(questionList, itemsArray) { for (var i = 0; i < itemsArray.length; i++) { switch (i ...

Sending AJAX data to the Controller for parsing and receiving the response

Check out this AJAX call that communicates with the controller: $.ajax({ type: "GET", url: '@Url.Action("getChat", "Chat")', success: function (data) { alert(data); $("#data").html(data); }, error:{ ...

Problem with selecting odd and even div elements

I have a div populated with a list of rows, and I want to alternate the row colors. To achieve this, I am using the following code: $('#PlatformErrorsTableData').html(table1Html); $('#PlatformErrorsTableData div:nth-child(even)').css(" ...

Having trouble choosing an option from the dropdown menu in bootstrap

I'm experiencing an issue with a bootstrap dropdown that I created. I am unable to select an item from it. The code is shown below: var aos = ''; $('.dropdown-item').click(function(event) { event.preventDefault(); // Prevents ...

When utilizing React router v5, the exact property in a route may not function as expected if there is a

I am attempting to structure routes like Vue.js in an array format: // routes.js export const routing = [ { path: "/setting", component: Setting, }, { path: "/", co ...

Using JavaScript to send formatted text through POST requests in a Rails application

Within APP A, I have the following formatted text: A beautiful painting because I created it because an artist endorsed it because my cat loves it I can access this formatted text in index.html.erb through product.body_html, which I then load into a Ja ...

Creating a classification for a higher order function

In the code snippet below, I have a controller that acts as a higher order method: const CourseController = { createCourse: ({ CourseService }) => async (httpRequest) => { const course = await CourseService.doCreateCourse(httpRequest. ...

Exploring the method to retrieve key value pairs within an array object using JavaScript

I have an array of JSON objects and I am trying to extract it as key-value pairs. Here is the code snippet: var fs = require('fs'); var parameters = { 'Tenantid': 'a62b57-a249-4778-9732', "packagecategoryn ...

What is the best way to arrange an array using AngularJs or Javascript?

When a user makes a selection, I want to sort and display an array in alphabetical order. Specifically, when we render data from the backend, I would like to display the fullName in alphabetical order. The $scope.selectedControlOwner is the ng-click event ...

Attempting to dynamically update the title of a Vue Meta page using a combination of a string and

In the blog application for my project using Nuxt JS 2.4.5, I am utilizing Vue Meta. I'm encountering difficulties while attempting to set the title along with a variable from data (), and it seems like something crucial is eluding me. Despite tryin ...

What steps should I follow to set JSONP as the dataType for a request in an Angular $http service?

I have a good understanding of how to use jQuery's $.ajax: $.ajax({ url: //twitter endpoint, method:"GET", dataType:"jsonp", success:function() { //stuff } }); Is there a way to set the JSONP datatype for an angular $http service reque ...

maintain ajax history during jquery redirection

Currently, I am designing a compact application using php, jquery, and ajax. The purpose of this app is to enable users to conduct customer searches, view customer details, and easily navigate back to the search page without losing any data. To enhance use ...

Exploring the concept of try-catch in JavaScript with a focus on the call stack within the Node

Recently, I delved into the world of Javascript and decided to explore how stack tracing functions in nodejs. Instead of looking at the source code, my lazy side led me to consult the language reference found at https://www.ecma-international.org/ecma-262/ ...

Reflection effect on the ground in three.js

Is there a way to achieve the same effect without duplicating spheres? I attempted to replicate the functionality, but the reflections appear too large :/ var groundTexture = THREE.ImageUtils.loadTexture( "files/checkerboard.jpg" ); groundTexture.wrapS = ...

Differences between Cross-domain Ajax calls and PHP requests

After reading up on cross-origin requests, I came across this resource link: https://coinmap.org/api/v1/venues/?mode=list It's interesting how I can successfully make a request to this link using PHP and retrieve data, but encounter an error when try ...

Is there a way to retrieve the objects generated by DirectionsRenderer on Google Maps V3?

Is there a simple method to access the objects and properties of the markers and infowindows that are generated by the DirectionsRenderer? (such as the "A" and "B" endpoints of the route) I want to swap out the infowindows for the "A" & "B" markers wi ...