问题
First time using MVC and Kendo Scheduler and I can't get the data to show on the Calendar. I have the below Model
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; }
}
My Controller just simply creates an instance of this class and adds data to it and returns a list as so:
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;
}
My view is simply this:
@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)
)
The calendar loads and switching from day to month and so on works, but no data will populate in the calendar. I've checked the model and it does have data. Is there something I am missing in order to get the data to show on the calendar? Any help would be greatly appreciated.
回答1:
Actually I figured it out what I was doing wrong. In the view under .DataSources(d => d.. blah blah blah, I needed to add a .Read("GetAll", "ControllerName"). In the Controller I then needed to add the Read Method with a Json(e.ToDataSourceResult(request), JsonRequestBehavior.AllGet); as shown below. I also needed to take out the code in the Index action result. Took care of the issue. See changes below:
View
@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)
)
Controller
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);
}
}
回答2:
In your ActionMethod, you need to change the return to:
return View(p); // where p is your model = List<Event>;
and then in the first line of the view, add:
@model List<Event>
来源:https://stackoverflow.com/questions/21285506/unable-to-populate-kendo-scheduler-with-data