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.