无法使用数据填充Kendo Scheduler

时间:2014-01-22 14:23:01

标签: c# asp.net-mvc-4 kendo-ui kendo-asp.net-mvc kendo-scheduler

首次使用MVC和Kendo Scheduler,我无法在日历上显示数据。我有以下模型

public class Events : ISchedulerEvent
    {
        public int Id { get; set; }

        public string Description { get; set; }

        public DateTime End { get; set; }

        public string EndTimezone { get; set; }

        public bool IsAllDay { get; set; }

        public string RecurrenceException { get; set; }

        public string RecurrenceRule { get; set; }

        public DateTime Start { get; set; }

        public string StartTimezone { get; set; }

        public string Title { get; set; }
    }

我的控制器只是简单地创建该类的一个实例并向其添加数据并返回一个列表:

public ActionResult Index()
        {
            return View(GetAll());
        }

        public List<Events> GetAll()
        {
            var p = new List<Events>();

            p.Add(new Events
            {
                Id = 1,
                Title = "Board Meeting",
                Start = DateTime.Now,
                End = DateTime.Now.AddHours(2)

            });

            return p;
        }

我的观点很简单:

@using Kendo.Mvc.UI;

@(Html.Kendo().Scheduler<Optic.Models.Calendar.Events>()
        .Name("scheduler")
        .Date(new DateTime(2014, 1, 22))
        .StartTime(new DateTime(2013, 6, 13, 07, 00, 00))
        .EndTime(new DateTime(2013, 6, 13, 21, 00, 00))
        .Editable(false)
        .Height(600)
        .Views(views =>
        {
            views.DayView();
            views.WeekView();
            views.MonthView(month => month.Selected(true));
            views.AgendaView();
        })
        .DataSource(d => d
        .Model(m => m.Id(f => f.Id))        
        )
        .BindTo(Model)
)

日历加载和日常切换等工作,但日历中不会填充任何数据。我检查了模型,它确实有数据。为了让数据显示在日历上,我有什么遗漏吗?任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:3)

实际上我弄清楚我做错了什么。在.DataSources下的视图中(d =&gt; d ...等等等等,我需要添加一个.Read(“GetAll”,“ControllerName”)。在Controller中我需要添加一个带有Json的Read方法( e.ToDataSourceResult(request),JsonRequestBehavior.AllGet);如下所示。我还需要取出Index动作结果中的代码。要注意这个问题。请参阅下面的更改:

查看

@using Kendo.Mvc.UI;
@model List<Optic.Models.Scheduling.Events>

@(Html.Kendo().Scheduler<Optic.Models.Scheduling.Events>()
        .Name("scheduler")
        .Date(new DateTime(2014, 1, 22))
        .StartTime(new DateTime(2013, 6, 13, 07, 00, 00))
        .EndTime(new DateTime(2013, 6, 13, 23, 00, 00))
        .Editable(false)
        .Height(600)
        .Views(views =>
        {
            views.DayView();
            views.WeekView(week => week.Selected(true));
            views.MonthView();
            views.AgendaView();
        })
        .DataSource(d => d
        .Model(m => m.Id(f => f.Id)) 
        .Read("GetAll", "Scheduling")       
        )

        .BindTo(Model)
)

控制器

 public class SchedulingController : Controller
    {
        //
        // GET: /Scheduling/

        public ActionResult Index()
        {
            return View();
        }

        public JsonResult GetAll([DataSourceRequest] DataSourceRequest request)
        {
            var e = new List<Events>
            {
                new Events
                {
                    Id =1,
                    Title="Testing 1",
                    Start= DateTime.Now.AddHours(1),
                    End = DateTime.Now.AddHours(2),
                    IsAllDay = false

                },
                new Events
                {
                    Id=2,
                    Title="Testing 2",
                    Start = DateTime.Now.AddHours(3),
                    End = DateTime.Now.AddHours(4),
                    IsAllDay = false
                }
            };

            return Json(e.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
        }

    }

答案 1 :(得分:0)

在ActionMethod中,您需要将返回值更改为:

return View(p); // where p is your model = List<Event>;

然后在视图的第一行添加:

@model List<Event>