我在全日历上显示事件时遇到问题。我正在使用 C# ASP.net 网络表单。我有 ashx 处理程序,我正在调用它以从数据库加载值,然后将其格式化为正在发回的 json 响应。下面是用于生成日历的javascript:
$(document).ready(function () {
// update Dialog
// page is now ready, initialize the calendar...
var date = new Date();
var d = date.getDate();
var m = date.getMonth();
var y = date.getFullYear();
var calendar = $('#calendar').fullCalendar({
// put your options and callbacks here
header:
{
left: 'title',
center: '',
right: 'month,agendaDay,agendaWeek, prev,next'
},
height: 600,
//contentHeight: auto,
titleFormat: 'MMMM D YYYY',
columnFormat: 'ddd D/M',
defaultView: 'agendaWeek',
handleWindowResize: true,
allDaySlot: true,
minTime: '09:00:00',
maxTime: '18:00:00',
slotLabelFormat: 'h(:mm)a',
slotLabelInterval: '01:00:00',
firstDay: 1,
weekends: false,
hiddenDays: [6, 7],
selectHelper: true,
select: selectDate,
editable: true,
eventDrop: eventDropped,
eventResize: eventResized,
events: {
url: 'JsonResponse.ashx',
color: 'yellow',
error: function () {
alert('Error while Getting events!');
}
},
eventRender: function (event, element) {
console.log("here");
//alert(event.title);
element.qtip({
content: event.description,
style: {
border: {
width: 1,
radius: 3,
color: '#2779AA'
},
padding: 10,
textAlign: 'center',
tip: true, // Give it a speech bubble tip with automatic corner detection
name: 'cream' // Style it according to the preset 'cream' style
},
error: function () {
alert('Error while Getting events!');
},
success: function () {
alert('Success!');
}
});
}
});
});
这是我在浏览器网络跟踪中得到的 JSON 响应:
"{"id":8,"title":"tester","start":"2018-01-30","end":">2018-01-30","全天":"false","description":"tester"}">
用于生成 JSON 的 ASHX 文件:
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "application/json";
DateTime start = new DateTime(1970, 1, 1);
DateTime end = new DateTime(1970, 1, 1);
start = Convert.ToDateTime(context.Request.QueryString["start"]);
end = Convert.ToDateTime(context.Request.QueryString["end"]);
String result = String.Empty;
result += ""[";
eventSerializer newEvent = new eventSerializer();
List<int> idList = new List<int>();
foreach (CalendarEvent cevent in EventDAO.getEvents(start, end))
{
String allDay = "true";
if (ConvertToTimestamp(cevent.start).ToString().Equals(ConvertToTimestamp(cevent.end).ToString()))
{
if (cevent.start.Hour == 0 && cevent.start.Minute == 0 && cevent.start.Second == 0)
{
allDay = "true";
}
else
{
allDay = "false";
}
}
else
{
if (cevent.start.Hour == 0 && cevent.start.Minute == 0 && cevent.start.Second == 0
&& cevent.end.Hour == 0 && cevent.end.Minute == 0 && cevent.end.Second == 0)
{
allDay = "true";
}
else
{
allDay = "false";
}
}
newEvent.id = cevent.id;
newEvent.title = cevent.title;
newEvent.start = cevent.start.ToString("yyyy-MM-dd");;
newEvent.end = cevent.end.ToString("yyyy-MM-dd");;
newEvent.allday = allDay;
newEvent.description = cevent.description;
}
JavaScriptSerializer js = new JavaScriptSerializer();
string jsonData = js.Serialize(newEvent);
context.Response.Write(jsonData);
}
public bool IsReusable
{
get
{
return false;
}
}
private long ConvertToTimestamp(DateTime value)
{
long epoch = (value.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
return epoch;
}
}
public class eventSerializer
{
public int id;
public string title;
public string start;
public string end;
public string allday;
public string description;
}
我试图将控制台.log添加到 eventRender 函数中,但它似乎没有被触发。事件也不会添加到日历中。我没有收到任何错误。
任何关于我出错的建议将不胜感激!
多亏了@ADyson,问题似乎与 JSON 未以正确的格式返回有关。将 JSON 添加到列表中并序列化列表后,事件将显示在日历上。有关更新的 ASHX,请参阅下文。
public class JsonResponse : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "application/json";
DateTime start = new DateTime(1970, 1, 1);
DateTime end = new DateTime(1970, 1, 1);
start = Convert.ToDateTime(context.Request.QueryString["start"]);
end = Convert.ToDateTime(context.Request.QueryString["end"]);
List<int> idList = new List<int>();
List<object> eventList = new List<object>();
foreach (CalendarEvent cevent in EventDAO.getEvents(start, end))
{
eventSerializer newEvent = new eventSerializer();
bool allDay = true;
if (ConvertToTimestamp(cevent.start).ToString().Equals(ConvertToTimestamp(cevent.end).ToString()))
{
if (cevent.start.Hour == 0 && cevent.start.Minute == 0 && cevent.start.Second == 0)
{
allDay = true;
}
else
{
allDay = false;
}
}
else
{
if (cevent.start.Hour == 0 && cevent.start.Minute == 0 && cevent.start.Second == 0
&& cevent.end.Hour == 0 && cevent.end.Minute == 0 && cevent.end.Second == 0)
{
allDay = true;
}
else
{
allDay = false;
}
}
idList.Add(cevent.id);
newEvent.id = cevent.id;
newEvent.title = cevent.title;
newEvent.start = cevent.start.ToString("yyyy-MM-dd HH:mm");
newEvent.end = cevent.end.ToString("yyyy-MM-dd HH:mm");
newEvent.allDay = allDay;
newEvent.description = cevent.description;
eventList.Add(newEvent);
}
//store list of event ids in Session, so that it can be accessed in web methods
context.Session["idList"] = idList;
JavaScriptSerializer js = new JavaScriptSerializer();
string jsonData = js.Serialize(eventList);
context.Response.Write(jsonData);
}
public bool IsReusable
{
get
{
return false;
}
}
private long ConvertToTimestamp(DateTime value)
{
long epoch = (value.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
return epoch;
}
}
public class eventSerializer
{
public int id;
public string title;
public string start;
public string end;
public bool allDay;
public string description;
}