1

Jsonを使用してFullCalendarにデータを読み込もうとしています。しかし、私はデータをロードできません:コントローラー:

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);
    }

そして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');
    }
});

何らかの理由で、jsonの結果が得られません。Jsonリクエスト

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: 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. インターネットアプリケーションテンプレートを使用して、新しいASP.NETMVC4アプリケーションを作成します
  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 に答える