Fullcalendar不呈现事件?

时间:2011-07-22 10:04:10

标签: asp.net fullcalendar

事件未在FullCalendar中呈现?

var calendar = $('#calendar').fullCalendar({
    theme: true,
    header: {
        left: 'prev,next today',
        center: 'title',
        right: 'month,agendaWeek,agendaDay'
    },
    //events: '/MyCalender/JsonResponse.ashx',
    events: [{id: '1',title: 'Event1',start:  1312462800,end: 1312462800,allDay:false,description: 'Event1'},{id: '3',title: 'Event2',start:  1309890600,end: 1309890600,allDay:true,description: 'Event2'},{id: '4',title: 'Event5',start:  1311705000,end: 1311705000,allDay:true,description: 'Event5'},{id: '5',title: 'Event3',start:  1310927400,end: 1310927400,allDay:true,description: 'Event3'},{id: '6',title: 'Event4',start:  1310495400,end: 1310495400,allDay:true,description: 'Event4'},{id: '7',title: 'Time Event1',start:  1312144200,end: 1312174800,allDay:false,description: 'Time Event1'}],
    ..other parameter            
        ...
});

我使用以下代码(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 = start.AddSeconds(double.Parse(context.Request.QueryString["start"]));
        end = end.AddSeconds(double.Parse(context.Request.QueryString["end"]));


        String result = String.Empty;

        result += "[";

        List<int> idList = new List<int>();
        foreach (CalendarEvent cevent in EventDAL.getEvents(start, end))
        {
            result += convertCalendarEventIntoString(cevent);
            idList.Add(cevent.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) + "'," +
                  "start:  " + ConvertToTimestamp(cevent.start).ToString() + "," +
                  "end: " + ConvertToTimestamp(cevent.end).ToString() + "," +
                  "allDay:" + allDay + "," +
                  "description: '" + HttpContext.Current.Server.HtmlEncode(cevent.description) + "'" +
                  "},";
    }


    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

    private long ConvertToTimestamp(DateTime value)
    {
        long epoch = (value.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
        return epoch;
    }  

2 个答案:

答案 0 :(得分:0)

经过一些试验和错误后,这里有一些观察结果:

名称和价值的双引号对我有用,例如。 “id”:“1”。代码中的名称没有结束双引号,值是单引号。试试双引号

开始日期和结束日期值必须以秒为单位。我不熟悉.net所以如果ConvertToTimestamp返回秒或毫秒,我就无法辨认出来。此值也应该是双引号。

您可以尝试以上建议,看看它们是否适合您?

答案 1 :(得分:0)

使用以下代码解决。

context.Response.ContentType = "application/json";

DateTime start = new DateTime(1970, 1, 1);
DateTime end = new DateTime(1970, 1, 1);

start = start.AddSeconds(double.Parse(context.Request.QueryString["start"]));
end = end.AddSeconds(double.Parse(context.Request.QueryString["end"]));

List<EventClass> listCalender = EventDAL.getEvents(start, end);

System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();
context.Response.Write(js.Serialize(listCalender));