I'm creating the fulcalendar in document ready and I have an asp button that runs the following C# function to fetch the data.
public void getAppointments()
{
List<Appoinment> appoinmentList = new List<Appoinment>();
AppointmentController appointmentController = new AppointmentController();
PublicUserProfileController publicUserProfileController = new PublicUserProfileController();
PublicUserProfile publicUserProfile = new PublicUserProfile();
//appoinmentList = appointmentController.fetchAppointmentByConsultent(Int32.Parse(Session["ICid"].ToString()));
appoinmentList = appointmentController.fetchAppointmentByConsultent(3);
var frontAppoinmentList = new List<object>();
foreach (var appoinment in appoinmentList)
{
publicUserProfile = publicUserProfileController.fetchPublicUserNameById(appoinment.PublicUserProfileId);
var name = publicUserProfile.FirstName + " " + publicUserProfile.LastName;
frontAppoinmentList.Add(new
{
id = appoinment.Id.ToString(),
title = name,
start = appoinment.UtcStartTime,
end = appoinment.UtcEndTime
});
}
// Serialize to JSON string.
JavaScriptSerializer jss = new JavaScriptSerializer();
String json = jss.Serialize(frontAppoinmentList);
var msg = String.Format("<script>loadCal('{0}');</script>", json);
//ClientScript.RegisterStartupScript(GetType(), "hwa", msg, true);
ScriptManager.RegisterClientScriptBlock(this, GetType(), "none", msg, false);
}
protected void btnmonthly_Click(object sender, EventArgs e)
{
getAppointments();
}
I have my JS to catch the JSON and load the events as,
function loadCal(eventList){
eventList = $.parseJSON(eventList);
alert(JSON.stringify(eventList));
for (var j = 0; j < eventList.length; j++) {
eventList[j].start = new Date(parseInt(eventList[j].start.replace("/Date(", "").replace(")/",""), 10));
eventList[j].end = new Date(parseInt(eventList[j].end.replace("/Date(", "").replace(")/",""), 10));
};
alert(JSON.stringify(eventList[0].start));
for (var j = 0; j < eventList.length; j++) {
var eId = eventList[0].id;
var eStart = eventList[0].start.toISOString();
var eEnd = eventList[0].end.toISOString();
var eTitle = eventList[0].title;
var event=
[{
id: eId,
title: eTitle,
start: eStart ,
end:eEnd
}];
$('#appCalendar').fullCalendar( 'renderEvent', event, true);
};
}
I'm creating the fullcalendar as this in document ready,
$('#appCalendar').fullCalendar({
header: {
left: 'prev,next today',
center: 'title',
right: ''
},
defaultDate: today,
defaultView: 'month',
editable: true
});
The render event doesnt render any events, but if I pass this in the console, the events gets rendered.
var event=[{ id:1, title:"manoj", start:"2017-06-15T22:30:00.000Z", end:"2017-06-15T23:30:00.000Z" }];
$('#appCalendar').fullCalendar( 'renderEvent', event, true);
Which is exactly What I expect for my loadCal function to do with the json I passed. These are the values set for the event array (eTitle,eId etc) when I checked through breakpoints in loadCal.
can someone please tell me why the events are not rendering? I've been at this for hours now.
Update I changed the C# to a webmethod as,
[WebMethod(EnableSession = true)]
public static string GetEvents()
{
List<Appoinment> appoinmentList = new List<Appoinment>();
AppointmentController appointmentController = new AppointmentController();
PublicUserProfileController publicUserProfileController = new PublicUserProfileController();
PublicUserProfile publicUserProfile = new PublicUserProfile();
//appoinmentList = appointmentController.fetchAppointmentByConsultent(Int32.Parse(Session["ICid"].ToString()));
appoinmentList = appointmentController.fetchAppointmentByConsultent(3);
var frontAppoinmentList = new List<object>();
foreach (var appoinment in appoinmentList)
{
publicUserProfile = publicUserProfileController.fetchPublicUserNameById(appoinment.PublicUserProfileId);
var name = publicUserProfile.FirstName + " " + publicUserProfile.LastName;
frontAppoinmentList.Add(new
{
id = appoinment.Id.ToString(),
title = name,
start = appoinment.UtcStartTime,
end = appoinment.UtcEndTime
});
}
// Serialize to JSON string.
JavaScriptSerializer jss = new JavaScriptSerializer();
String json = jss.Serialize(frontAppoinmentList);
return json;
}
and my Jquery to,
$(document).ready(function () {
$('#appCalendar').fullCalendar({
eventClick: function() {
alert('a day has been clicked!');
},
events: function (start, end, callback) {
$.ajax({
type: "POST", //WebMethods will not allow GET
url: "AppointmentDiary.aspx/GetEvents", //url of a webmethod - example below
//completely take out 'data:' line if you don't want to pass to webmethod - Important to also change webmethod to not accept any parameters
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (doc) {
var events = []; //javascript event object created here
var obj = $.parseJSON(doc.d); //.net returns json wrapped in "d"
$(obj).each(function () {
var startd= new Date(parseInt(this.start.replace("/Date(", "").replace(")/",""), 10));
var endd = new Date(parseInt(this.end.replace("/Date(", "").replace(")/",""), 10));
events.push({
title: this.title, //your calevent object has identical parameters 'title', 'start', ect, so this will work
start:startd.toISOString(), // will be parsed into DateTime object
end: endd.toISOString(),
id: this.id
});
});
//if(callback) callback(events);
//$('#appCalendar').fullCalendar( 'renderEvent',events[0], true);
alert(JSON.stringify(events[0]));
}
});
return events;
}
});
});
Callback becomes 'false' when this runs but I can see that the webmethod returning this in the alert after formatting the dates,
I'm getting the data from the aspx, I can read it in the jquery and I still cant render an even.at this point I don't know what eles to do. can you please look at my code and point out whats wrong? also I don't understand the use of (start,end,callback) in the function as I dont use that in the webmethod anyway.
loadCal(eventList)
function called onclick
? – Curiousdev