【发布时间】:2017-06-16 12:03:15
【问题描述】:
我正在准备好文档中创建 fulcalendar,并且我有一个运行以下 C# 函数来获取数据的 asp 按钮。
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);
};
}
我正在创建完整的日历,准备好文档,
$('#appCalendar').fullCalendar({
header: {
left: 'prev,next today',
center: 'title',
right: ''
},
defaultDate: today,
defaultView: 'month',
editable: true
});
渲染事件不会渲染任何事件,但如果我在控制台中传递它,事件就会被渲染。
var event=[{ id:1, title:"manoj", start:"2017-06-15T22:30:00.000Z", 结束:“2017-06-15T23:30:00.000Z”}];
$('#appCalendar').fullCalendar('renderEvent', event, true);
这正是我期望我的 loadCal 函数对我传递的 json 所做的。这些是我在 loadCal 中检查断点时为事件数组(eTitle、eId 等)设置的值。
谁能告诉我为什么事件没有呈现?我已经在这里工作了好几个小时了。
更新 我将 C# 更改为 web 方法,
[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;
}
});
});
回调在运行时变为“假”,但我可以看到网络方法在格式化日期后在警报中返回它,
我正在从 aspx 中获取数据,我可以在 jquery 中读取它,但我仍然无法渲染一个 even。此时我不知道该怎么做 eles。你能看看我的代码并指出什么问题吗?我也不明白函数中 (start,end,callback) 的使用,因为我在 webmethod 中也不使用它。
【问题讨论】:
-
你的
loadCal(eventList)函数是在click上调用的吗? -
loadCal 最有可能在 fullCalendar 创建日历对象之前调用。您需要稍微重组您的 js 以确保正确的顺序
标签: c# jquery asp.net json fullcalendar