我在文檔準備中創建了fulcalendar,並且我有一個運行以下C#函數來獲取數據的asp按鈕。事件不會呈現給來自C#Fullcalendar的事件
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();
}
我有我的JS趕上JSON並加載事件,
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);
};
}
我創建了fullcalendar,因爲這在文件準備好,
$('#appCalendar').fullCalendar({
header: {
left: 'prev,next today',
center: 'title',
right: ''
},
defaultDate: today,
defaultView: 'month',
editable: true
});
渲染事件不會渲染任何事件,但是如果我在控制檯中傳遞這些事件,事件就會被渲染。
變種事件= [{ID:1,標題: 「的Manoj」,啓動: 「2017-06-15T22:30:00.000Z」, 端:「2017-06-15T23:30:00.000Z 「}]; ('#appCalendar')。fullCalendar('renderEvent',event,true);
這正是我期望的loadCal函數與我通過的json做的事情。當我通過loadCal中的斷點檢查時,這些是爲事件數組(eTitle,eId等)設置的值。
有人可以告訴我爲什麼事件不呈現?我現在已經呆了好幾個小時了。
更新 我改變了C#到一個WebMethod的,
[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;
}
和我的jQuery來,
$(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;
}
});
});
回調變爲 '假' 時,這個運行,但我可以看到, webmethod在格式化日期後將其返回到警報中,
我從aspx獲取數據,我可以在jQuery中讀取它,但我仍然無法渲染一個even.at這一點,我不知道它可以做什麼。你可以看看我的代碼,並指出最新的錯誤?我也不明白在函數中使用(開始,結束,回調),因爲我不在webmethod中使用它。
是你的'loadCal(EVENTLIST)'調用'click'的函數? fullCalendar創建日曆對象之前 – Curiousdev
loadCal是最有可能調用。您需要重構您的js一點,以確保正確的順序 – Andrei