>我正在尝试从我的数据库中获取事件并用这些事件填充整个日历。事件未显示 。尽管当我在控制器中创建用于测试的事件时,它们会显示
控制器:在这种情况下不显示事件
public JsonResult getEvents()
{
UserProfile user = getUserProfile();
List<Calendar> events = db.Calendars.Where(c => c.userID == user.userID).ToList();
IList<calendarPost> tasksList = new List<calendarPost>();
foreach (Calendar eve in events)
{
tasksList.Add(new calendarPost
{
id = eve.recordID,
title = eve.recordDescription,
start = ToUnixTimespan(eve.startTime),
end = ToUnixTimespan(eve.endTime),
url = "www.google.com"
// });
// }
});
}
return Json(tasksList.ToArray(), JsonRequestBehavior.AllowGet);
如果我这样做,事件将显示:
public JsonResult getEvents()
{
UserProfile user = getUserProfile();
List<Calendar> events = db.Calendars.Where(c => c.userID == user.userID).ToList();
IList<calendarPost> tasksList = new List<calendarPost>();
// foreach (Calendar eve in events)
// {
tasksList.Add(new calendarPost
{
id = 5,
title = "test",
start = ToUnixTimespan(DateTime.Now.AddHours(1)),
end = ToUnixTimespan(DateTime.Now.AddHours(2)),
url = "www.google.com"
// });
// }
});
return Json(tasksList.ToArray() , JsonRequestBehavior.AllowGet);
}
是的,顺便说一句,两者都产生相同的 Json 输出
[{"id":5,"title":"test","start":1336945877,"end":1336949477,"url":"www.google.com","backgroundColor":null}]
[{"id":1,"title":"Testing the Calendar","start":1349384400,"end":1352062800,"url":"www.google.com","backgroundColor":null},{"id":3,"title":"Testing Votes","start":1325748600,"end":1325752200,"url":"www.google.com","backgroundColor":null}]
您可以尝试添加 jsonresponse.ashx(处理程序)文件,并将以下代码添加到该文件中,
jsonresponse.ashx
using System;
using System.Web;
using System.Collections;
using System.Collections.Generic;
using System.Web.SessionState;
public class JsonResponse : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
DateTime start = new DateTime(2012, 4, 1);
DateTime end = new DateTime(2012, 4, 30);
String result = String.Empty;
result += "[";
List<long> idList = new List<long>();
List<Calendar> events = db.Calendars.Where(c => c.userID == user.userID).ToList();
foreach (Calendar eve in events)
{
result += convertCalendarEventIntoString(eve);
idList.Add(eve.id);
}
if (result.EndsWith(","))
{
result = result.Substring(0, result.Length - 1);
}
result += "]";
//store list of event ids in Session, so that it can be accessed in web methods
context.Session["idList"] = idList;
context.Response.Write(result);
}
private String convertCalendarEventIntoString(CalendarEvent cevent)
{
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";
}
}
return "{" +
"id: '" + cevent.id + "'," +
"title: '" + HttpContext.Current.Server.HtmlEncode(cevent.title) + "'," +
"description: '" + HttpContext.Current.Server.HtmlEncode(Convert.ToString(cevent.description)) + "'," +
"start: " + ConvertToTimestamp(cevent.start).ToString() + "," +
"end: " + ConvertToTimestamp(cevent.end).ToString() + "," +
"allDay:" + allDay + "," +
"description: '" + HttpContext.Current.Server.HtmlEncode(cevent.description) + "'," +
"url: '" + HttpContext.Current.Server.HtmlEncode(cevent.url) + "'" +
"},";
}
private long ConvertToTimestamp(DateTime value)
{
long epoch = (value.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
return epoch;
}
}