1

I know similar questions have been asked before but none of the solutions worked for me.

I have a rather strange issue while passing list of objects to controller method using ajax. It works when I have a limited set of data (64 records in the list), but as soon as I increase the size of list, I get null value in controller method.

I have following view model that holds list data:

public class CalendarListViewModel
{
    public int ID { get; set; }

    [DataType(DataType.Date)]
    public DateTime CalendarDate { get; set; }
    public string CalendarDay { get; set; }
    public string CurrentMonth { get; set; }
    public bool IsToday { get; set; }
    public bool IsPurchaseOrderDay { get; set; }
    public string Holiday { get; set; }
    public bool IsTier1DeliveryDay { get; set; }
    public bool IsTier2DeliveryDay { get; set; }
    [DataType(DataType.Date)]
    public DateTime Tier1DeliveryEndDate { get; set; }
    [DataType(DataType.Date)]
    public DateTime Tier2DeliveryEndDate { get; set; }
    public bool AddLeftBorder { get; set; }
    public string Tier1HolidayText { get; set; }
    public string Tier2HolidayText { get; set; }

}

In the get method i populate the list from database and return to the view.

Then i'm trying to pass that list back to the controller method to perform pagination and setup calendar, using ajax call.

Below is how i'm parsing the list:

var calendarList = JSON.parse('@Html.Raw(Json.Serialize(Model.CalendarListViewModel))');

Then on page load, i'm calling the ajax method:

$(document).ready(function () {         LoadCalendarForDesktop(pageNumberForDesktop, pageSizeForDesktop, calendarList, pageCounter); //Load calendar for Desktop view.. });  

Ajax method is rather lengthy, so i will share the relevant piece of code:

 
    //Ajax call to Load Calendar..
    function LoadCalendarForDesktop(pageNumber, pageSize, calendarList, pageCounter) {
        debugger;
        $.ajax({
            type: 'POST',
            url: '@Url.Action("LoadCalendar", "LPProductManagement")',
            data: { calendarList },
            cache: false,
            async: false,
            success: function (data) {
                try {

                    let calendarMonth = '';
                    let calendarDays = '';
                    let calendarDetailsTop = '';
                    let calendarDetailsBottom = '';

Everything works well when i only load limited set of data (64 records).

enter image description here

As soon as i load 2 more pages of data, I get null for list value in the controller method. Even though i can see the list has values by putting the debugger in ajax method.

enter image description here

enter image description here

The data is pretty straight forward so i doubt if there is something wrong with the data since i have tried to hardcode some values and still i get the same issue.

Is there a limitation to how much data can be parsed using JSON.parse? I don't know what i'm missing here, if someone could point me in the right direction, that will be a great help.

Thanks.

7
  • "Is there a limitation to how much data can be parsed using JSON.parse" - no (other than memory limits, in which case an error would occur rather than just null) Commented Sep 10, 2024 at 8:48
  • 1
    I suggest you could try this codes inside the progame.cs to increase the formoption limit builder.Services.Configure<FormOptions>(options => { options.ValueCountLimit = int.MaxValue; options.MultipartBodyLengthLimit = long.MaxValue; });. Commented Sep 10, 2024 at 8:49
  • Why are you not performing pagination on the server/database in the first place rather than getting a list then sending it back? Working with a smaller data set from the start could be a simple and quick solution to your problem. Commented Sep 10, 2024 at 10:55
  • @BrandoZhang I already have this piece of code in Startup.cs Commented Sep 10, 2024 at 13:32
  • @Peppermintology that will not work in my case, since i need the full list to render the calendar every time user navigates back and forward. I thought it best to have the full list retrieved on page load and do all the processing afterwards, so i don't have to go to the database again. Commented Sep 10, 2024 at 13:34

1 Answer 1

0

So, following changes worked for me. If we look at below payload for the request, it was being sent as a query string.

enter image description here

So i changed existing ajax method with below changes:

        //Ajax call to Load Calendar..
function LoadCalendarForDesktop(pageNumber, pageSize, calendarList, pageCounter) {
    $.ajax({
        type: 'POST',
        url: '@Url.Action("LoadCalendar", "LPProductManagement")',
        contentType: "application/json",
        dataType: "json",
        data: JSON.stringify({ pageNumber, pageSize, calendarList, pageCounter }),
        cache: false,
        async: false,
        success: function (data) {
            try {

                let calendarMonth = '';
                let calendarDays = '';
                let calendarDetailsTop = '';

And then in the code behind, I had to use [FromBody]. While using [FromBody] be sure to specify application/json content type.

So, I had to change following method signature: enter image description here

With below changes:

        [HttpPost]
    public JsonResult LoadCalendar([FromBody] CalendarPayloadViewModel calendarPayloadViewModel)
    {
        int pageNumber;
        int pageSize;
        List<CalendarListViewModel> calendarList = new List<CalendarListViewModel>();
        int pageCounter = 0;

        CalendarGridViewModel calendarViewModel = new CalendarGridViewModel();
        bool hasNextRecord = false; // to disable/enable next chevron icons..
        bool hasPreviousRecord = false; // to disable/enable previous chevron icons..

Now if you execute the method, you can see all the values being passed correctly along with the list:

enter image description here

And finally, if you look at the payload now, you can see it in correct json format:

enter image description here

I hope this helps anyone who is facing similar issues.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.