简体   繁体   中英

Show events in fullcalendar using ajax

I am writing application on 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. 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. It is empty!

GetEvents method in Schedules controller which gets data from DB

    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.

<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". 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 
  }) 
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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