简体   繁体   English

使用Ajax以全日历显示事件

[英]Show events in fullcalendar using ajax

I am writing application on asp.net core 2.0. 我在asp.net core 2.0上编写应用程序。 and trying to show events stored in DB in calendar. 并尝试显示日历中存储在数据库中的事件。 I have added links to js and css files as it is shown in documentation. 我已经添加了指向js和css文件的链接,如文档中所示。 As event source I am using events as function as it is shown in official documentation Fullcalendar Doc but my calendar does not show events. 作为事件源,我使用事件作为功能,如正式文档Fullcalendar Doc中所示,但我的日历未显示事件。 It is empty! 它是空的!

GetEvents method in Schedules controller which gets data from DB Schedules控制器中的GetEvents方法,该方法从数据库获取数据

    public ActionResult GetEvents()
    {
        return new JsonResult(Json(from events in _context.Schedules
                                   select new { id = events.ID,
                                                EventTitle = events.EmployeeID,
                                                ShiftDate = events.ShiftDate.Date } ));
    }

I also tried to modify ajax but it also does not work. 我也尝试修改ajax,但它也无法正常工作。

<div id="calendar"></div>

<script>
    $(document).ready(function () {
        $('#calendar').fullCalendar({
             events: function (start, end, timezone, callback) {
                $.ajax({
                    url: "/Schedules/GetEvents",
                    type: "POST",
                    dataType: "JSON",

                    success: function (result) {
                        var eventsList = [];
                        // alert(result);                 // --1--
                        // alert(JSON.stringify(result)); // --2--
                        $(result).each(function () {
                            var eventTitle = $(this).attr('EventTitle');
                            var eventStart = $(this).attr('ShiftDate');
                            var eventId = $(this).attr('id');
                            eventsList.push(
                                {
                                    id: eventId,
                                    title: eventTitle,
                                    start: eventStart
                                });
                        });
                        if (callback)
                            callback(eventsList);
                    }
                });
            }

        });
    });
</script>

First alert shows message [Object object]... , Second alert shows all selected data in a "JSON string". 第一个alert显示消息[Object object]... ,第二个alert显示“ JSON字符串”中的所有选定数据。 But calendar does not show anything. 但是日历不显示任何内容。 So, what am I doing wrong? 那么,我在做什么错呢? Thank you! 谢谢!

Try this: 尝试这个:

result.forEach(function (item) { 
  eventsList.push({ 
    id: item.id, title: item.EventTitle, start: item.ShiftDate 
  }) 
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM