pass list of objects to controller method using ajax .net core

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

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

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:

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:

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

I hope this helps anyone who is facing similar issues.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật