更新事件时添加ASP.Net MVC 4 Kendo UI Scheduler多个事件

时间:2014-02-10 09:08:16

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

我是剑道大学的新手。我在ASP.Net MVC 4项目中使用了调度程序控件,并使用下面教程中定义的Ajax调用实现。

http://docs.telerik.com/kendo-ui/getting-started/using-kendo-with/aspnet-mvc/helpers/scheduler/ajax-editing

我的问题是当我尝试编辑事件时,它会编辑事件,并在数据库中使用相同的数据创建一个或多个重复记录。我该怎样避免它。

以下是我的Index.cshtml

@(Html.Kendo().Scheduler<Portal.Presentation.Web.BoundedContext.QC.MVC.Areas.Razor.Models.LeavePlan>()
  .Name("scheduler")
.Date(DateTime.Now)
.Height(600)
.AllDaySlot(true)
.Views(views =>
{
    views.DayView();
    views.WeekView(weekView => weekView.Selected(true));
    views.MonthView();
})
.Timezone("Etc/UTC")
.Resources(resource =>
{
    resource.Add(m => m.PlanTypeId)
        .Title("Plan Type")
        .DataTextField("Text")
        .DataValueField("Value")
        .DataColorField("Color")
        .BindTo(new[] { 
            new { Text = "Annual Leave", Value = 1, Color = "#f8a398" } ,
            new { Text = "Casual Leave", Value = 2, Color = "#51a0ed" } ,
            new { Text = "Sick Leave", Value = 2, Color = "#55a098" } ,
            new { Text = "Travel", Value = 3, Color = "#56ca85" } 
        });
})
.DataSource(d => d
    .Model(m => {
        m.Id(f => f.TaskID);
        m.Field(f => f.ResourceId).DefaultValue(1);
        //Set the recurrence ID field from the model:
        //m.RecurrenceId(f => f.RecurrenceID);
    })
    .Read("Tasks_Read", "LeavePlan")
    .Create("Tasks_Create", "LeavePlan")
    .Destroy("Tasks_Destroy", "LeavePlan")
    .Update("Tasks_Update", "LeavePlan")
))

以下是我的更新方法。

  public ActionResult Tasks_Update([DataSourceRequest]DataSourceRequest request, LeavePlan leave)
    {
        if (ModelState.IsValid)
        {

                // Create a new Task entity and set its properties from the posted TaskViewModel
            var entity = new ResourceLeavePlan
                {
                    StartDate = leave.Start
                    ,Title = leave.Title
                    ,EndDate = leave.End.ToUniversalTime()
                    ,ResourceLeavePlanId = (int)leave.TaskID
                    ,IsAllDay=leave.IsAllDay
                    ,RecurrenceId=leave.ResourceId
                    ,Description=leave.Description
                    ,RecurrenceException=leave.RecurrenceException
                    ,LeaveTypeId=(int)leave.PlanTypeId
                    ,ResourceId = resourceId
                };

            _resourceLeavePlanService.Update(entity);
            }

        return Json(new[] { leave }.ToDataSourceResult(request, ModelState));
    }

对此的帮助将不胜感激。

该模型与上述链接教程中的模型相同。

如果有人能解释我在调度程序中使用“RecurrenceId”,也会感激不尽

1 个答案:

答案 0 :(得分:1)

您在实体对象中缺少唯一的 TaskID

    var entity = new ResourceLeavePlan
    {

    TaskID = leave.TaskID // Just try adding this line please and it would work

    ,StartDate = leave.Start
    ,Title = leave.Title
    ,EndDate = leave.End.ToUniversalTime()
    ,ResourceLeavePlanId = (int)leave.TaskID
    ,IsAllDay=leave.IsAllDay
    ,RecurrenceId=leave.ResourceId
    ,Description=leave.Description
    ,RecurrenceException=leave.RecurrenceException
    ,LeaveTypeId=(int)leave.PlanTypeId
    ,ResourceId = resourceId
    };