"Troubleshooting: Why isn't my MVC5 Controller able to receive

I am facing an issue with a controller method that I have defined:

public JsonResult Save(List<BlogInfo> list)
{
    return Json(new { Data = "" }, JsonRequestBehavior.AllowGet);
}

In addition, there is an ajax post request from the client side like this:

$.ajax({
    url: "Home/Save",
    type: "post",
    contentType: "application/json",
    data: ko.mapping.toJSON(ViewModel),
    success: function (response) {
        alert(response.Status);
    }
});

The problem is that the 'list' parameter in the controller always ends up being null. Even when I tried changing it to a string instead of List, it had the same outcome.

Upon inspecting the JSON payload using Fiddler, I noticed that it is structured as follows:

{"Data":[{"Id":1,"Title":"Sudoku Solver","Description":"Programmed Sudoku Solver","Tags":["programming","sudoku"]},{"Id":2,"Title":"Picnic","Description":"Went to millpoint","Tags":["picnic"]},{"Id":0,"Title":"Title","Description":"Description","Tags":[]}]}

Answer №1

The example of JSON provided does not appear to be in array format, therefore it cannot be directly bound to a server-side list. To fix this issue, ensure that the client is sending an array of objects:

data: ko.mapping.toJSON(ViewModel.Data);

In the code snippet above, we are using the ViewModel.Data property which contains an array structure to send out the desired JSON structure:

[{"Id":1,"Title":"Sudoku Solver","Description":"Programmed Sudoku Solver","Tags":["programming","sudoku"]},{"Id":2,"Title":"Picnic","Description":‌​"Went to millpoint","Tags":["picnic"]},{"Id":0,"Title":"Title","Description":"Description‌​","Tags":[]}]

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

Creating a customized URL using the famous "@" symbol

Is it possible to set up a dynamic route in Next.js that includes the @ symbol? For instance, localhost:3000/@some_username I'm familiar with creating dynamic routes using the pages folder, but I'm not sure how to achieve this specific format w ...

The Angular $rootScope.user.userMessage throws an error stating that it is not an object

I am encountering an issue: Error: The object '$rootScope.user.userMessage' is not defined. (evaluating 'typeof $rootScope.user.userMessage') This error arises from the following code snippet: if (typeof($rootScope.user.userMessage ...

Using jQuery to Send Data Back to Parent Popup Window

Is there a way to achieve the following functionality with jQuery: create a popup window that sends a value back to the main window when a link in the popup is clicked, then closes the popup and automatically submits a form in the main window based on the ...

What is the best way to change the height of a div element as the user scrolls using JavaScript exclusively?

Coding with JavaScript var changeHeight = () => { let flag = 0; while(flag != 1) { size += 1; return size; } if(size == window.innerHeight/2){ flag == 1; } } var size = 5; document.body.style.height = &qu ...

Encountering an Issue Executing Selenium Test on jQuery v2.0.2 and Play Framework

While I may not be a selenium expert, it seems that I've stumbled upon a bug when trying to utilize jQuery v2.0.2 with my Play Framework 2.2.1 application instead of the default jQuery v.1.9.0. Whenever I run "play test", I encounter the following err ...

Service is not recognizing the content-type application/json specified in RestAssured

Utilizing jQuery in Google Chrome with --disable-web-security, I am able to access a service using the following code snippet: $.ajax({ type: 'POST', url: "http://10.30.1.2:9234/myapp/v6/token/generate", headers: { "Content-T ...

What's the best way to incorporate necessary styles into text using particular fonts?

Is there a way to set letter spacing and word spacing for text on my website with the font 'classylight'? Adding classes to each post in the site map seems like a lengthy process. To save time, I attempted to use the attribute*=style property to ...

Laravel has not been properly initialized

Recently, I've been exploring Laravel 5.3 and vue.js and I'm trying to make a GET request to fetch some user data from my database. I'm utilizing components in this project. Here is a snippet from my app.js file: require('./bootstrap ...

Adding an image to a React component in your project

I am currently working on an app that utilizes React and Typescript. To retrieve data, I am integrating a free API. My goal is to incorporate a default image for objects that lack images. Here is the project structure: https://i.stack.imgur.com/xfIYD.pn ...

Transferring a JSON array from one activity to another using intents

I had a similar question before, but the solution didn't work for me. So I decided to ask again. My issue is with passing a JSONarray from one activity to another using an intent. In my code, I connect to a MySQL database and retrieve user data whic ...

Verify key in JSON object using org.json.simple library in Java

Is there a way to convert a key into a JSONObject using the org.json.simple library? I've tried a few methods but encountered errors in each attempt. 1st Attempt JSONObject name1 = (JSONObject) jsonObject.get(key); Error: cannot convert java.lan ...

Removing characters extracted from a Json with an EditText

Can I extract only the date part from a string obtained from a JSONObject response in an EditText field? For example, if the string is '2019-02-10T00: 00: 00', can I display only '2019-02-10'? ...

Modify the color or background color of a disabled Material UI checkbox

The disabled unchecked checkbox appears too subtle to me, and I would like to enhance it by giving it a grey background and changing the cursor style to not-allowed. I've been trying to implement these styles on the checkbox using the makeStyles, but ...

Unable to execute node in vscode terminal

Can anyone help me figure out why I'm unable to run Node in my vscode terminal? I'm a newbie and currently using POP OS 22.04. https://i.stack.imgur.com/WqHWM.pnghttps://i.stack.imgur.com/GEHeA.png Your assistance would be greatly appreciated. ...

What is the best way to enable and disable the edit-in-place feature using jQuery?

Recently, I decided to experiment with the jQuery In Place Editor but I've come across an issue I can't seem to solve. Below is the code snippet that I have been working on. In my implementation, I am utilizing the jQuery toggle method to enable ...

Utilizing MVC6 to send both a CSV file and a string to a controller via Ajax

I'm encountering an issue that involves sending a CSV file along with the value of a string variable back to the controller. Individually, I've been successful in sending the file via the form's submit button and the string variable via aja ...

What is the best way to retrieve a list of fields from a set of JSON documents?

Currently, I am facing a scenario where I have two MongoDB collections named stu_creds and stu_profile. My objective is to initially fetch all the student records from the stu_creds collection where the stu_pref_contact field corresponds to email. Subseque ...

How can one properly utilize material-ui CSS?

Just starting out with material-ui and react, I'm currently following this palette customization guide from Material-UI. However, when attempting to replicate the example shown, I encountered the error: Cannot read property 'prepareStyles' ...

When the browser is not in the foreground, clicking on the Bootstrap datepicker with Selenium does not register

When you click on the input field <input id="dp1" class="span2" type="text" value="02-16-2012"> If the browser is in the background, the datepicker popup will not display. Even using javascript or jquery to click the input field does not show the ...

Determining the minimum and maximum values of a grid using props in a React component

I have created a code for a customizable grid screen that is functioning perfectly. However, I am facing an issue where I want the minimum and maximum size of the grid to be 16 x 100. Currently, when a button is clicked, a prompt window appears asking for ...