MVC does not have the capability to parse JSON data

Here is a snippet of my C# code:

Model:

namespace WebInventory.Models
{
    [DataContract]
    public class PostbackList
    {
        [DataContract]
        public class Field
        {
            public int ID;
            public string Name;
            public int DataTypeID;

            public bool Checked;
        }

        [DataMember]
        public int TypeID;

        [DataMember]
        public IList<Field> Fields;

        [DataMember]
        public IList<IList<string>> Values;

        #region Non DataMember
        public IList<int> UsedFieldsID;
        #endregion
    }
}

Controller:

public class ListController : JsonController
{
    [HttpPost]
    public ActionResult GetList(PostbackList data)
    {
        <...>
    }
}

When sending the JSON object below:

{"TypeID":16,"Fields":[{"ID":42,"Name":"","DataTypeID":0,"Checked":true},{"ID":43,"Name":"","DataTypeID":0,"Checked":true},{"ID":44,"Name":"","DataTypeID":0,"Checked":true}],"Values":null}

using this AJAX call:

    $.ajax({
        url: url
        , type: 'POST'
        , dataType: 'json'
        , contentType: 'application/json; charset=utf-8'
        , data: JSON.stringify(parameter)
        , xhrFields: {
            withCredentials: true
        }
    })
    .done(function (data) {
        fillTemplate(data);
    });

I have encountered an issue where data appears to be uninitialized in the MVC GetList. I tried using arrays instead of IList but it didn't resolve the problem.

I am currently unsure what the cause of this issue may be.

Answer №1

In my opinion, utilizing properties over fields is the way to go.

Recently, I encountered a similar issue where the model binder was not properly capturing my JSON data.

To resolve this, I made the switch to using properties instead.

public int ID {get; set;}

I recommend converting all your fields to properties as shown below.

namespace WebInventory.Models
{
    [DataContract]
    public class PostbackList
    {
        [DataContract]
        public class Field
        {
            public int ID {get; set;}
            public string Name {get; set;}
            public int DataTypeID {get; set;}

            public bool Checked {get; set;}
        }

        [DataMember]
        public int TypeID {get; set;}

        [DataMember]
        public IList<Field> Fields {get; set;}

        [DataMember]
        public IList<IList<string>> Values {get; set;}

        #region Non DataMember
        public IList<int> UsedFieldsID {get; set;}
        #endregion
    }
}

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

Rotating 3D cube with CSS, adjusting height during transition

I'm attempting to create a basic 2-sided cube rotation. Following the rotation, my goal is to click on one of the sides and have its height increase. However, I've run into an issue where the transition occurs from the middle instead of from the ...

Calculate the sum of numbers entered in a textarea using JavaScript for each line break

I'm working with a textarea that contains the following text: link|10000 link|25000 link|58932 My goal is to eliminate everything before the "|" on each line and then calculate the total sum of all the numbers. If anyone can provide assis ...

I am looking to transform intricate json data into csv format with the help of either python or R programming

Converting values to rows in CSV and keys to columns in CSV format would be beneficial. { "_id": { "$uId”: “12345678” }, “comopany_productId”: “J00354”, “`company_product name`”: “BIKE 12345”, "search_resu ...

Troubleshooting module not being found with rekuire, requirish, or rfr in resolving relative require problem in nodejs

Looking to steer clear of the complicated relative path problem detailed here by opting for one of the suggested solutions. I've found three similar libraries that could help: rekuire node-rfr aka Require from Root requirish I've experimented ...

What is the best way to retrieve seat number upon clicking in React Native?

I have implemented a for loop and assigned its value in the click function to retrieve the seat number when a seat is clicked. However, I am only getting the last number of the for loop every time a seat is clicked. Can someone guide me on how to obtain th ...

"Extending Material-UI: Utilizing Multiple Snackbar Components

I've been struggling to display multiple warnings using Snackbar from the Material UI library in my React project. I haven't found any examples with React, only Vue. Can anyone help me with this? Here is the code that I have tried so far: https: ...

What is the best way to sort my API responses to display only users who are either currently online or offline?

Hi everyone, I've made great progress on my project so far without any assistance (pretty proud of myself), but now I could use some help. I'm working on creating a tabbed menu that filters the results of my API calls to display: All users, Onlin ...

Issue with displaying Angular chart only resolves after resizing window following routing updates

Hey there! I'm having trouble getting my chart to show up after adding routing to my 3 tabs. Previously, without routing, everything worked fine. Now, the graph only appears after resizing the window. The chart is using PrimeNG chart with the Chart.js ...

What is the reason for using a string as the index of an array in the following code?

var arrayOfNumbers = [1, 2, 3, 4, 5, 6, 78]; for(var index in arrayOfNumbers){ console.log(index+1); } The result produced by the given code snippet is as follows: 01 11 21 31 41 51 61 What is the reason behind JavaScript treating these ...

Securing your folders with Next Auth middleware

I am currently developing a NextJS application and have implemented routers at pages/dashboard/* that I need to secure. My goal is to restrict access to these routes only to authenticated users. I am utilizing Prisma for data management, with Google as the ...

Generate a fresh object if the values within the TypeScript object are identical

How can I filter an object to return a new object containing elements with the same values? For example: allValues = {"id1": 3, "id2": 4, "id3": 3} The desired output is: filteredValues = {"id1": 3, "id3": 3} This is because the keys "id1" and "id3" hav ...

Create a Python function that takes a list as input, iterates through each element, converts it to

When working with a Python list that requires double quotes around the strings in order to pass it to an API as a list of double quote strings, I encountered an issue. The data needed by the API: data = { "styles" : styleList } It worked when I manu ...

Guide to sending a POST request from within my application

I'm facing an issue with using $resource for making a post request in my app. Here is the code snippet from my Angular module: angular.module('myApp').factory('Product', ['$resource', function ($resource) { ...

Integrating Dynamics CRM with an External Source to Trigger Workflows

Scenario: Imagine a scenario where you want to trigger an existing workflow or a custom action from a webpage located outside the CRM Dynamics environment, such as MS CRM 2011-2013-2015-2016 and 365. Potential Solution: One possible solution could be to ...

Issue with image button click event not triggering consistently across all pages in Site.master

I am encountering a strange issue with my Site.master page. The onserverclick event works perfectly fine when I load certain .aspx pages, but for some reason, it does not fire at all on other pages. This is quite puzzling and I would appreciate any insight ...

The issue of jQuery file upload not functioning on every row of an ASP.NET repeater

Incorporating a jQuery file upload button in each repeater row has resulted in a display issue where only the selected files for the first row are shown. For subsequent rows, only the total count of selected files is displayed without the progress bar sh ...

I encountered an error message saying "TypeError: response.json is not a function" while attempting to make a request using fetch in a project involving ReactJS and Node

Currently, I am in the process of developing a webpage using reactjs and have set up a simple REST api/database with nodejs. My main focus right now is on properly utilizing this API from the front end. The objective was to send a JSON payload containing { ...

Updating the content of a cell in a pandas dataframe with a JSON value

Currently, I am in the process of replacing a specific value within a cell of a pandas dataframe that I am constructing from JSON data obtained through the Airtable API. The structure of the JSON file resembles the snippet provided below: [ { & ...

Angular binding and date validation - a powerful combination!

Facing an issue with date properties in Angular. For instance, here is the Model I am working with: export class Model{ dateFrom Date; dateTo Date; } In my Create view, I have the following setup: <input type="date" [(ngModel)] = "model.dateFrom"> ...

Changes in a deep copy of an object in a child component are directly reflected in the parent object in VueJS

Let's begin by discussing the layout. I have a page dedicated to showcasing information about a specific company, with a component Classification.vue. This component displays categories of labels and the actual labels assigned to the current company. ...