eventRender没有被触发Fullcalendar ASP.net C#webforms

时间:2018-01-30 19:11:45

标签: javascript c# asp.net json fullcalendar

我在fullcalendar上显示事件时遇到问题。我正在使用带有C#的ASP.net webforms。我有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,"标题":"测试""开始":" 2018年1月30日""端":" 2018年1月30日""阿迪":"假",& #34;描述":"测试"}"

用于生成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;
}

我试图将console.log添加到eventRender函数中,它似乎没有被触发。事件也没有添加到日历中。我没有收到任何错误。

对于我出错的地方的任何建议都将不胜感激!

1 个答案:

答案 0 :(得分:1)

感谢@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;
}