无法将数据绑定到Kendo Scheduler

时间:2014-12-01 12:05:41

标签: kendo-ui kendo-asp.net-mvc kendo-scheduler

我有一个在视图中显示但没有任何数据的Kendo Scheduler。

视图上的调度程序:

@(Html.Kendo().Scheduler<ProjName.Models.ScheduleInspectionModel>()
    .Name("scheduler")
    .Views(views =>
    {
        views.DayView();
        views.WorkWeekView();
        views.WeekView();
        views.MonthView(mv => mv.Selected(true));
        views.AgendaView();
    })
    .Timezone("Etc/UTC")
    .DataSource(d => d
        .Read("ControllerName", "GetScheduleInspections")
    )
)

数据源调用下面的控制器方法:

public ActionResult GetScheduleInspections([DataSourceRequest]DataSourceRequest request)
{
    ScheduleInspectionModel sim = new ScheduleInspectionModel();
    var gsio = sim.getScheduleInspections();

    List<ScheduleInspectionModel> list = new List<ScheduleInspectionModel>();

    if (gsio.scheduleinspections != null)
    {
        foreach (wsScheduleInspection.scheduleInspectionOutput scheduleInspection in gsio.scheduleinspections)
        {
            ScheduleInspectionModel sim2 = new ScheduleInspectionModel
            {
                GlobalEquipConditionId = scheduleInspection.globalEquipmentCondition.id,
                Description = scheduleInspection.globalEquipmentCondition.code,
                Start = DateTime.Now,
                End = DateTime.Now.AddHours(2),
                Title = scheduleInspection.globalEquipmentCondition.code,
                IsAllDay = true

            };

            list.Add(sim2);
        }
    }
    return Json(list.ToDataSourceResult(request));
}

但是这个方法永远不会运行,尽管它位于Scheduler Datasource属性上。它应该运行该方法并返回检查列表。我不知道为什么这个方法不被击中。例如,使用Kendo Grid,只要页面加载,就会点击数据源读取的方法。

2 个答案:

答案 0 :(得分:3)

尝试确保您的定义包含这两项,因为我认为是必需的。

.Date(new DateTime(2013, 6, 13))
.StartTime(new DateTime(2013, 6, 13, 7, 00, 00))

修改

我能够获得以下代码:

<强>模型

// NOTE: It's important that your model class implements ISchedulerEvent
public class TaskViewModel : ISchedulerEvent
{
    public string Title { get; set; }
    public string Description { get; set; }
    public bool IsAllDay { get; set; }
    public DateTime Start { get; set; }
    public DateTime End { get; set; }
    public string StartTimezone { get; set; }
    public string EndTimezone { get; set; }
    public string RecurrenceRule { get; set; }
    public string RecurrenceException { get; set; }
}

<强> SchedulerController.cs

public class SchedulerController : Controller
{
    // GET: Scheduler
    public ActionResult Index()
    {
        var model = new SchedulerViewModel();

        // In this case, it doesn't matter what this model is really since we're using AJAX binding
        return View(model);
    }

    // I usually have my binding methods for Kendo use HttpPost
    [HttpPost]
    public ActionResult GetData([DataSourceRequest] DataSourceRequest request)
    {
        var data = new List<TaskViewModel>
            {
                new TaskViewModel
                    {
                        Start = new DateTime(2014, 12, 1, 8, 0, 0),
                        End = new DateTime(2014, 12, 1, 17, 0, 0),
                        Title = "Task 1"
                    }
            };

        return Json(data.ToDataSourceResult(request));
    }
}

Index.cshtml(查看)

@(Html.Kendo().Scheduler<TaskViewModel>()
    .Name("scheduler")
    .Views(views =>
    {
        views.DayView();
        views.WorkWeekView();
        views.WeekView();
        views.MonthView(mv => mv.Selected(true));
        views.AgendaView();
    })
    .Timezone("Etc/UTC")
    .DataSource(d => d
        .Read("GetData", "Scheduler")
    ))

如果这对您不起作用,我会确保您的版本(对于Kendo,jQuery等)是正确的。希望这会有所帮助。

答案 1 :(得分:0)

是的,调度程序读取一旦被调用就会被调用。但它可能没有像预期的那样以正确的格式获取数据。所以它无法绑定数据。如果您可以检查这些修改:

1)在此example中定义的调度程序的“DataSource”中定义“模型”。

2)action方法也应该返回“MyModel”类的对象(定义了调度程序的模型)
   不是“ScheduleInspectionModel”类。