1

I'm trying to load data into an FullCalendar, using Json. But i can't load the data: The controller:

ppublic ActionResult GetEvents()
    {
        List<Models.Events> events = new List<Models.Events>()
    {     
        new Models.Events("Fremvising","2013-01-11T14:08:00Z", "2013-01-11T16:09:00Z", false),
        new Models.Events("Fremvising","2013-01-12T15:09:00Z", "2013-01-11T17:10:00Z", false),
        new Models.Events("Fremvising","2013-01-13T16:10:00Z", "2013-01-11T18:11:00Z", false),
        new Models.Events("Fremvising","2013-01-14T17:11:00Z", "2013-01-11T19:12:00Z", false),
        new Models.Events("Fremvising","2013-01-15T18:12:00Z", "2013-01-11T20:13:00Z", false),                                   
        new Models.Events("Fremvising","2013-01-16T19:13:00Z", "2013-01-11T21:14:00Z", false)

    };
        return Json(events, JsonRequestBehavior.AllowGet);
    }

And the javascript:

$(document).ready(function () {


    /* initialize the external events
    -----------------------------------------------------------------*/

    $('#external-events div.external-event').each(function () {

        // create an Event Object (http://arshaw.com/fullcalendar/docs/event_data/Event_Object/)
        // it doesn't need to have a start or end
        var eventObject = {
            title: $.trim($(this).text()) // use the element's text as the event title
        };

        // store the Event Object in the DOM element so we can get to it later
        $(this).data('eventObject', eventObject);

        // make the event draggable using jQuery UI
        $(this).draggable({
            zIndex: 999,
            revert: true,      // will cause the event to go back to its
            revertDuration: 0  //  original position after the drag
        });

    });


    /* initialize the calendar
    -----------------------------------------------------------------*/

    $('#calendar').fullCalendar({
        header: {
            left: 'prev,next today',
            center: 'title',
            right: 'agendaWeek,agendaDay',
            height: 650,
            // url:,
        }, events: [
            $.getJSON("@Url.Action("GetEvents")", function (locationsArray) {
                $.each(locationsArray, function (index, location) {

                    title  : location.title;
                    start: location.start;
                    end: location.end;
                    allDay: location.editable; // will make the time show

                });
            })
             ],

        allDaySlot: false,
        //minTime: 10,
        //maxTime: 21,
        dayNames: ['Søndag', 'Mandag', 'Tirsdag', 'Onsdag',
       'Torsdag', 'Fredag', 'Lørdag'],
        dayNamesShort: ['Søn', 'Man', 'Tirs', 'Ons', 'Tors', 'Fre', 'Lør'],
        editable: true,
        defaultView: 'agendaWeek',
        droppable: true, // this allows things to be dropped onto the calendar !!!
        drop: function (date, allDay) { // this function is called when something is dropped

            // retrieve the dropped element's stored Event Object
            var originalEventObject = $(this).data('eventObject');

            // we need to copy it, so that multiple events don't have a reference to the same object
            var copiedEventObject = $.extend({}, originalEventObject);

            // assign it the date that was reported
            copiedEventObject.start = date;
            copiedEventObject.allDay = allDay;

            // render the event on the calendar
            // the last `true` argument determines if the event "sticks" (http://arshaw.com/fullcalendar/docs/event_rendering/renderEvent/)
            $('#calendar').fullCalendar('renderEvent', copiedEventObject, true);

            // is the "remove after drop" checkbox checked?
            if ($('#drop-remove').is(':checked')) {
                // if so, remove the element from the "Draggable Events" list
                $(this).remove();
            }

        }
    });
});
$('#tabs').tabs({
    show: function (event, ui) {
        $('#calendar').fullCalendar('render');
    }
});

For some reason it doesnt get the json result. The Json request

0: {title:Fremvising, start:2013-01-11T14:08:00Z, end:2013-01-11T16:09:00Z, editable:false}
1: {title:Fremvising, start:2013-01-12T15:09:00Z, end:2013-01-11T17:10:00Z, editable:false}
2: {title:Fremvising, start:2013-01-13T16:10:00Z, end:2013-01-11T18:11:00Z, editable:false}
3: {title:Fremvising, start:2013-01-14T17:11:00Z, end:2013-01-11T19:12:00Z, editable:false}
4: {title:Fremvising, start:2013-01-15T18:12:00Z, end:2013-01-11T20:13:00Z, editable:false}
5: {title:Fremvising, start:2013-01-16T19:13:00Z, end:2013-01-11T21:14:00Z, editable:false}
4

1 回答 1

6

您错误地定义了eventsFullCalendar 的属性。您已将其分配给 javascript 数组,但这是不正确的,因为您的数据来自服务器。您应该将其设置为匿名函数。此外,您似乎正在触发对服务器的 AJAX 请求,但在成功回调中,除了循环遍历它们之外,您对结果完全没有做任何事情,但您从未将它们传递给 FullCalendar。这是定义 events 属性的正确方法:

events: function (start, end, callback) {
    $.getJSON("@Url.Action("GetEvents")", function (locationsArray) {
        var result = $(locationsArray).map(function () {
            return {
                title: this.title,
                start: this.start,
                end: this.end,
                allDay: this.editable
            };
        }).toArray();
        callback(result);
    });
},

请注意我们如何对来自服务器的结果执行转换(如果您使用了视图模型,这当然不是必需的),然后我们callback使用这个转换后的结果集调用属性。

好的,这是一个分步指南:

  1. 使用 Internet 应用程序模板创建一个新的 ASP.NET MVC 4 应用程序
  2. 将 HomeController 修改为如下所示:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
    
        public ActionResult GetEvents()
        {
            var events = new[]
            {     
                new { title = "Fremvising", start = "2013-01-11T14:08:00Z", end = "2013-01-11T16:09:00Z", editable = false },
                new { title = "Fremvising", start = "2013-01-12T15:09:00Z", end = "2013-01-11T17:10:00Z", editable = false },
                new { title = "Fremvising", start = "2013-01-13T16:10:00Z", end = "2013-01-11T18:11:00Z", editable = false },
                new { title = "Fremvising", start = "2013-01-14T17:11:00Z", end = "2013-01-11T19:12:00Z", editable = false },
                new { title = "Fremvising", start = "2013-01-15T18:12:00Z", end = "2013-01-11T20:13:00Z", editable = false },                                   
                new { title = "Fremvising", start = "2013-01-16T19:13:00Z", end = "2013-01-11T21:14:00Z", editable = false }
            };
            return Json(events, JsonRequestBehavior.AllowGet);
        }
    }
    
  3. 下载FullCalendar并将其放入Scripts文件夹

  4. 修改~/Views/Shared/_Layout.cshtml为如下所示:

    <!DOCTYPE html>
    <html lang="en">
        <head>
            <meta charset="utf-8" />
            <title>@ViewBag.Title - My ASP.NET MVC Application</title>
            @RenderSection("styles", required: false)
        </head>
        <body>
            @RenderBody()
            @Scripts.Render("~/bundles/jquery")
            @RenderSection("scripts", required: false)
        </body>
    </html>
    
  5. 就像这样~/Views/Home/Index.cshtml

    <div id="calendar"></div>
    
    @section styles {
        <link href="~/scripts/fullcalendar-1.5.4/fullcalendar/fullcalendar.css" rel="stylesheet" />
    }
    
    @section scripts {
        <script type="text/javascript" src="~/scripts/fullcalendar-1.5.4/fullcalendar/fullcalendar.js"></script>
        <script type="text/javascript">
            $('#calendar').fullCalendar({
                header: {
                    left: 'prev,next today',
                    center: 'title',
                    right: 'agendaWeek,agendaDay',
                    height: 650,
                }, 
                events: function (start, end, callback) {
                    $.getJSON("@Url.Action("GetEvents")", function (locationsArray) {
                        var result = $(locationsArray).map(function () {
                            return {
                                title: this.title,
                                start: this.start,
                                end: this.end,
                                allDay: this.editable
                            };
                        }).toArray();
                        callback(result);
                    });
                },
    
                allDaySlot: false,
                dayNames: ['Søndag', 'Mandag', 'Tirsdag', 'Onsdag', 'Torsdag', 'Fredag', 'Lørdag'],
                dayNamesShort: ['Søn', 'Man', 'Tirs', 'Ons', 'Tors', 'Fre', 'Lør'],
                editable: true,
                defaultView: 'agendaWeek',
                droppable: true,
                drop: function (date, allDay) { // this function is called when something is dropped
    
                    // retrieve the dropped element's stored Event Object
                    var originalEventObject = $(this).data('eventObject');
    
                    // we need to copy it, so that multiple events don't have a reference to the same object
                    var copiedEventObject = $.extend({}, originalEventObject);
    
                    // assign it the date that was reported
                    copiedEventObject.start = date;
                    copiedEventObject.allDay = allDay;
    
                    // render the event on the calendar
                    // the last `true` argument determines if the event "sticks" (http://arshaw.com/fullcalendar/docs/event_rendering/renderEvent/)
                    $('#calendar').fullCalendar('renderEvent', copiedEventObject, true);
    
                    // is the "remove after drop" checkbox checked?
                    if ($('#drop-remove').is(':checked')) {
                        // if so, remove the element from the "Draggable Events" list
                        $(this).remove();
                    }
    
                }
            });
        </script>
    }
    
于 2013-01-13T08:36:51.573 回答