I am trying to fetch a set of data from controller to view, I have the following;
Index.cshtml
<input type="button" value="submit" class="btn btn-default" id="btnGet" name="btnGet" onclick="displayCalendar()" />
Upon pressing the button, the displayCalendar() function calls the jQuery fullCalendar which populates all the events from the controller.
The problem is that I have a set of integers that I also want to be fetched in that AJAX call or separately.
Controller:
return Json(totalList, JsonRequestBehavior.AllowGet);
Now what have I tried? I tried using ViewBag, ViewData, TempData. like this;
TempData["countWeekDays"] = countWeekDays;
TempData["countPresents"] = countPresents;
TempData["countAbsence"] = countAbsence;
TempData["countLates"] = countLates;
TempData["countFines"] = countFines;
and then display them in the same Index.cshtml but it did not work. I, then made a list of integers and tried to bind that list along with the already list of objects that was passed in the json like;
List<int> statsView = new List<int>();
statsView.Add(countWeekDays);
statsView.Add(countPresents);
statsView.Add(countAbsence);
statsView.Add(countLates);
statsView.Add(countFines);
IEnumerable<object> completeObjects = totalList.Cast<object>().Concat(statsView.Cast<object>());
return Json(completeObjects, JsonRequestBehavior.AllowGet);
When I tried the above method, I could see the list in the console debug in the browser but the problem is that it doesnt populate the calendar then. if I exclude the list statsView then the calendar is properly populated with the events.
Maybe a function within the displayCalendar() that is when called, it fetches the list from the controller and simply displays it in the view?
UPDATED:
<div id='calendar'></div>
@section scripts{
<script>
function h() {
id = $('#EmpId').val();
s = $("#startDate").val();
e = $("#endDate").val();
$.post("/Controller/method",
{
ENum: id,
StartDate: s,
EndDate: e
},
function (response) {
callCalendar(response);
}
);
}
function displayCalendar(e)
{
$("#calendar").fullCalendar('removeEvents');
$("#calendar").fullCalendar('addEventSource', e);
$("#calendar").fullCalendar('rerenderEvents');
$('#calendar').fullCalendar({
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaDay'
},
}
});
//stats here
}
</script>
<script>
$(document).ready(function () {
displayCalendar('');
});
</script>
}
statsViewthat I want to display on the view. (debugged, the values are in it just not being able to take to the view)