Sending an array of intricate elements from a webpage to a server-side handler through Ajax requests

I am facing an issue in the controller where I need to receive two parameters (detail and test). One parameter is a List of custom objects, while the other is a string. However, when I try to pass both parameters, I only manage to pass one (the list of objects) and end up receiving null values in the controller.

Below is the resulting JSON being sent to the controller:

[{
    "detail": [{
        "tag": "PIC330_620%2F_.PV_Out%23Value",
        "color": "%2331c63e"
    }, {
        "tag": "A330_10%2F_.FbkFwdOut%23Value",
        "color": "%238edeed"
    },
    // additional entries truncated for brevity
    ],
    "test": "lolo"
}]
//Generate list of objects
function getViewDetail() {
    // function implementation details omitted for brevity
}

// Call Ajax
function sendParameters(){
    // function implementation details omitted for brevity
}

//In the controller (abbreviated)
public JsonResult SaveView(IEnumerable<Detail> detail, string test)
{}

//class
public class Detail
{
    // class properties definition omitted for brevity
}

Answer №1

Here is a helpful suggestion for you:

data = { detail: details, test: 'lolo' };
data = JSON.stringify(data);

Next, make sure to send it through ajax like this:

data: data,

Your action requires two parameters, detail and test. You were passing a list of objects with these properties instead. To clarify, your object should be structured like this when posting:

{
    "detail": [...],
    "test": "lolo"
}

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

JSON parsing problem in web browsers

I've been working on an application using Asp.net MVC. When I submit the form using jQuery: var Data = $("#frmForgotPassword").serialize(); $.post("<%= Url.Action("ForgotPassword","Account") %>/", Data, function(retdata, textStatus) { if ( ...

I'm having trouble locating my route for some unknown reason

I've set up a basic code structure to test my routes, but I keep getting a 404 error. Although I have an app.all function to catch errors, I'm having trouble pinpointing where the issue lies. Below is my index.js file, where I've configured ...

Create eye-catching banners, images, iframes, and more!

I am the owner of a PHP MySQL website and I'm looking to offer users banners and images that they can display on their own websites or forums. Similar to Facebook's feature, I want to allow users to use dynamic banners with links. This means the ...

Python code: Retrieving data from PostgreSQL and converting it to a JSON format

My task involves fetching camera details from PostgreSQL and presenting them in a single JSON response. However, I am facing difficulty in achieving this as the current method I'm using processes one line at a time with for row in self.data:. How can ...

Create a plot marker on a map for every user's location based on their IP

Within my database, there exists a users table that stores each user's IP address. Additionally, I have an API that is capable of retrieving the latitude and longitude coordinates for these users. Initially, the goal is to fetch the latitude and long ...

Instructions on how to navigate back one page to determine which page initiated the action

I am looking for a solution to implement a back button that takes me back one page while caching it. Currently, I am using the following code: <a id="go-back" href="javascript:history.go(-1)" type="button" class="btn btn-danger">Back</a> Howe ...

Why does a Drone CI job abruptly end before a Selenium-based npm script is completed?

I currently have a drone set up and my pipeline is configured to do the following: pipeline: test: image: node:8.3.0 commands: - npm install --only=dev - npm run automation The automation script in my package.json looks like this: ...

The error message appears as "TypeError: json cannot be iterated

Encountering an Issue C:\Development\AlphaLauncher-Recode\app\assets\js\loggerutil.js:29 [Launcher] TypeError: json is not iterable at DistroIndex._resolveInstances (C:\Development\AlphaLauncher-Recode\app& ...

Retrieving the value from a vuetify v-text-field to use in another text field

Looking to suggest a username based on the user's first and last name. The concept is that after the user fills out the first name and last name fields, the value in the username field will be a combination of their first and last names. firstName = B ...

Use jQuery to create a price filter that dynamically sets slide values based on the filter object

I'm currently utilizing the jQuery slide filter to sort products based on price. The filtering value is set within the script, but I am interested in dynamically setting it based on the highest/lowest number found on my page using a data attribute. D ...

What is the best way to determine if a variable is an object array or not?

I need to determine whether a variable is an Object array or not. Consider the following example data: var myColumnDefs = [ {key:"label", sortable:true, resizeable:true}, {key:"notes", sortable:true,resizeab ...

Concealing two div elements based on string content

I am working on a unique Wordpress blog post layout that showcases personnel profile cards, displaying 12 individuals at a time. Depending on whether or not a person has a Twitter handle entered in the backend, certain elements should either be shown or hi ...

Personalizing the TabMenu or TabView Components in Angular with PrimeNg

Description: Currently tackling a project that involves recreating a widget similar to TabMenu/TabView in PrimeNg. Struggling with implementing different routerlinks on each button and customizing it to be more dynamic or resemble the provided image. Des ...

How to retrieve the length of data in Angular without relying on ng-repeat?

I am currently working with Angular and facing a challenge where I need to display the total length of an array without using ng-repeat. Here is the situation: I have a default.json file: { { ... "data": [{ "name":"Test", "erro ...

Locate specific phrases within the text and conceal the corresponding lines

I have a JavaScript function that loops through each line. I want it to search for specific text on each line and hide the entire line if it contains that text. For example: <input id="search" type="button" value="Run" /> <textarea id ...

A guide on reading columns in a Postgres database with a data type of BYTEA and compressed JSON encoding using Python

Looking for guidance on extracting values from a column in a postgres database that is of data type bytea with compressed_json encoding. How can I accurately retrieve the JSON data using Python? ...

Switch between viewing outcomes retrieved from a database

I'm fairly new to working with jQuery, PHP and databases, but I have managed to successfully create a database and retrieve data using PHP. When a search term is entered, the retrieved data from the database is displayed on the results page. For exam ...

Identifying line breaks caused by browsers or CSS forced line breaks

<p style="width:60px"> This is just a sample text. It is a piece of text that doesn't really say anything meaningful.</p> When this text is rendered as HTML, it would look like this: This is just a sample text. It is a piece of text ...

Decoding JSON arrays on iOS devices

While following a tutorial on parsing json objects, I came across the following code snippet: - (void)connectionDidFinishLoading:(NSURLConnection *)connection { NSLog(@"connectionDidFinishLoading"); NSLog(@"Succeeded! Received %d bytes of data",[s ...

Using Gson library to handle complex JSON structures in an Android Studio project

My JSON file looks like this: {"code":0,"datas":[{"id":0,"target_id":0},{"id":1,"target_id":0}................]} In my code, I have written the following: // String data; // data contains the JSON data as a string JsonParser jsonParser = new JsonParse ...