即使使用refetchEvents和rerenderEvents,Fullcalendar也不会重新加载

时间:2018-11-30 14:55:30

标签: asp.net-mvc fullcalendar

我已经搜索了关于此主题的stackoverflow,并尝试了解决方案,但是它对我没有用。即使重新加载整个页面,在我终止程序并重新启动程序之前,日历中都不会显示任何添加的事件。我知道这是通过代码进行的,因为我在refetchEvents和rerenderEvents行上都遇到了断点。

以下是javascript:

$(document).ready(function () {

    var TheCalendar = $('#ODCalendar').fullCalendar({
        header:
            {
                left: 'prev,next today',
                center: 'title',
                right: 'month,agendaWeek,agendaDay'
            },
        buttonText: {
            today: 'today',
            month: 'month',
            week: 'week',
            day: 'day'
        },

        events: function (start, end, timezone, callback) {
            $.ajax({
                url: '/Home/GetCalendarData',
                type: "GET",
                dataType: "JSON",

                success: function (result) {
                    var events = [];

                    $.each(result, function (i, data) {
                        var bColor = "#000000";
                        var tColor = "#FFFFFF";
                        switch (data.ReleaseType) {
                            case '1':
                                if (data.Action == '1') {
                                    bColor = '#cc9900';
                                }
                                else if (data.Action == '2') {
                                    bColor = '#ffd24d';
                                    tColor = "#000000";
                                }
                                else {
                                    bColor = '#e6e6e6';
                                }
                                break;
                            case '2':
                                if (data.Action == '1') {
                                    bColor = '#ff8080';
                                }
                                else if (data.Action == '2') {
                                    bColor = '#ffcccc';
                                    tColor = "#000000";
                                }
                                else {
                                    bColor = '#e6e6e6';
                                }
                                break;
                            case '3':
                                if (data.Action == '1') {
                                    bColor = '#0099ff';
                                }
                                else if (data.Action == '2') {
                                    bColor = '#ccebff';
                                    tColor = "#000000";
                                }
                                else {
                                    bColor = '#e6e6e6';
                                }
                                break;
                            case '4':
                                if (data.Action == '1') {
                                    bColor = '#2fb62f';
                                }
                                else if (data.Action == '2') {
                                    bColor = '#d6f5d6';
                                    tColor = "#000000";
                                }
                                else {
                                    bColor = '#e6e6e6';
                                }
                                break;
                            case '99':
                                bColor = '#cccccc';
                                break;
                            default:
                                bColor = '#999999';
                        }

                        events.push(
                            {
                                title: data.Title,
                                description: data.Description,
                                start: moment(data.Start_Date).format('YYYY-MM-DD HH:mm'),
                                end: moment(data.End_Date).format('YYYY-MM-DD HH:mm'),
                                url: '/Home/ShowEditEvent?id=' + data.id + '&Owner=' + data.Owner,
                                textColor: tColor,
                                backgroundColor: bColor,
                                borderColor: "#000066"
                            });
                    });

                    callback(events);
                }
            });
        },

        eventRender: function (event, element) {
            element.qtip(
                {
                    content: event.description
                });
        },

        editable: true
    });
    // Refresh each time home page loads
    $('#ODCalendar').fullCalendar('refetchEvents');
    $('#ODCalendar').fullCalendar('rerenderEvents');
    });

在MVC控制器中创建数据。我使用HTML.BeginForm而不是AJAX.BeginForm,因为它给我带来的麻烦超过了它的价值:

public ActionResult AddEvent(Guid CalendarID = new Guid(), string Title = "",
            int Owner = 0, string Description = "", string Start_Date = "", string End_Date = "",
            string ReleaseType = "", string ReleaseVersion = "", string Action = "", string Network = "", string CRQ = "",
            string Comments = "", string StoreList = "", bool SharedEntry = true)
        {
            CalendarEntry model = new CalendarEntry();

            try
            {
                using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["CalendarConnection"].ToString()))
                {
                    string query = "Insert Into CalendarEntry (id, CalendarID, Title, Owner, Description, Start_Date, End_Date, ReleaseType, ReleaseVersion, Action, Network, CRQ, Comments, StoreList, SharedEntry)" +
                        " Values(@id, @CalendarID, @Title, @Owner, @Description, @Start_Date, @End_Date, @ReleaseType, @ReleaseVersion, @Action, @Network, @CRQ, @Comments, @StoreList, @SharedEntry)";

                    SqlCommand command = new SqlCommand(query, connection);
                    command.Parameters.AddWithValue("id", id);
                    command.Parameters.AddWithValue("CalendarID", CalendarID);
                    command.Parameters.AddWithValue("Title", Title);
                    command.Parameters.AddWithValue("Owner", Owner);
                    command.Parameters.AddWithValue("Description", Description);
                    //command.Parameters.AddWithValue("Start_Date", Start_Date);
                    //command.Parameters.AddWithValue("End_Date", End_Date);
                    command.Parameters.AddWithValue("Start_Date", Start_DateDT);
                    command.Parameters.AddWithValue("End_Date", End_DateDT);
                    command.Parameters.AddWithValue("ReleaseType", ReleaseType);
                    command.Parameters.AddWithValue("ReleaseVersion", ReleaseVersion);
                    command.Parameters.AddWithValue("Action", Action);
                    command.Parameters.AddWithValue("Network", Network);
                    command.Parameters.AddWithValue("CRQ", CRQ);
                    command.Parameters.AddWithValue("Comments", Comments);
                    command.Parameters.AddWithValue("StoreList", StoreList);
                    command.Parameters.AddWithValue("SharedEntry", SharedEntry);
                    connection.Open();
                    command.ExecuteNonQuery();
                }
            }
            catch
            {

            }
            return Redirect("Index");
        }

我要回到日历所在的“索引”页面,它会通过javascript代码-但不会重新加载它。请问我哪里出问题了?

1 个答案:

答案 0 :(得分:0)

最终找到了答案。实际上,我是两天前发现的,但是将代码行放在错误的位置。这次我找到了正确的地方。只需添加一行:

cache: false

在这里:

events: function (start, end, timezone, callback) {
        $.ajax({
            url: '/Home/GetCalendarData',
            type: "GET",
            dataType: "JSON",
            cache: false,

            success: function (result) {
                var events = [];

等...

谢谢您的帮助。

  • 约翰C