Kendo UI Scheduler错误地调用了WebAPI

时间:2018-12-04 19:04:10

标签: asp.net-core kendo-ui asp.net-core-2.1 kendo-ui-mvc

我一直在Telerik论坛和Stackoverflow周围寻找答案,但我完全陷入困境,无法找出问题所在。

我将Kendo UI用于Asp.Net Core Scheduler控件。我可以从我的控制器中读取数据。但是,我无法正确调用HttpPut处理程序。

检查流量时,我得到以下响应,因此我的HttpPut处理程序内的断点将永远不会被击中。

400 - Bad Request
{"":["The input was not valid."]}

我认为我的代码是:

@(Html.Kendo().Scheduler<MeetingViewModel>()
      .Name("SchedulerView")
      .Height(500)
      .Date(DateTime.Now.ToUniversalTime())
      .StartTime(new DateTime(2018, 11, 28, 0, 00, 00).ToUniversalTime())
      .MajorTick(30)
      .ShowWorkHours(false)
      .Footer(false)
      .Editable(edit =>
      {
          //edit.Resize(false);
          edit.Create(false);
      })
      .Views(views =>
      {
          views.TimelineView(timeline => timeline.EventHeight(50));
          //views.TimelineWeekView(timeline => timeline.EventHeight(50));
          //views.TimelineWorkWeekView(timeline => timeline.EventHeight(50));
          //views.TimelineMonthView(timeline =>
          //{
          //    timeline.StartTime(DateTime.Now);
          //    timeline.EndTime(DateTime.Now.AddMonths(1));
          //    timeline.MajorTick(1440);
          //    timeline.EventHeight(50);
          //});

      })
      .Timezone("Etc/UTC")
      .Group(group => group.Resources("WorkCenters" /*,"Attendees"*/).Orientation(SchedulerGroupOrientation.Vertical))
      .Resources(resource =>
      {
          resource.Add(m => m.ScheduleRowID)
              .Title("Work Center")
              .Name("WorkCenters")
              .DataTextField("Text")
              .DataValueField("Value")
              .DataColorField("Color")
              .BindTo(@Model.AvailableWorkCenters);
      })
      .DataSource(d => d
          .ServerOperation(true)
          .WebApi()
          .Model(m =>
          {
              m.Id(f => f.ActivityID);
              m.Field(f => f.Title).DefaultValue("No title");
              //m.RecurrenceId(f => f.RecurrenceID);
              m.Field(f => f.Description).DefaultValue("No Description");
          })
          .Events(events => events.Error("error_handler"))
          .Read(read => read.Action("GetActivities", "Scheduler").Data("setRequestDateTimes"))
          //.Create(create => create.Action("Post", "Scheduler"))
          .Update(update => update.Action("PutActivity", "Scheduler", new { id = "{0}" }).Type(HttpVerbs.Put))
          //.Destroy(destroy => destroy.Action("Delete", "Scheduler", new { id = "{0}" }))
      )))

我的API控制器如下:

[Route("Api/[controller]")]
[ApiController]
public class SchedulerController : DawnController
{
    public SchedulerController(DatabaseContext context) : base(context)
    {
    }

    [HttpGet]
    public DataSourceResult GetActivities([DataSourceRequest] DataSourceRequest request, DateTime requestStartDateTime, DateTime requestEndDateTime)
    {
        //Kendo doesnt seem to send the full date range. so + 1 day to end
        requestEndDateTime = requestEndDateTime.AddDays(1);

        List<MeetingViewModel> test = new List<MeetingViewModel>();

        foreach (JobTask jobTask in Context.JobTask)
        {
            if (JobTask.HasActivityInDateRange(jobTask, requestStartDateTime, requestEndDateTime))
            {
                foreach (Activites jobTaskAct in jobTask.Activites)
                {
                    test.Add(new MeetingViewModel()
                    {
                        JobTaskID = jobTask.JobTaskId,
                        ActivityID = jobTaskAct.ActivityId,
                        Title = jobTaskAct.Name,
                        Description = jobTaskAct.Description,
                        Start = jobTaskAct.StartTime.ToUniversalTime(),
                        End = jobTaskAct.EndTime.ToUniversalTime(),
                        IsAllDay = false,
                        ScheduleRowID = jobTaskAct.Workcenter.WorkCenterId,
                    });
                }
            }
        }
        return test.ToDataSourceResult(request);
    }


    [HttpPut("{id}")]
    public IActionResult PutActivity(int id, MeetingViewModel task)
    {
        if (ModelState.IsValid && id == task.ActivityID)
        {
            try
            {
                //breakpoint here
                bool a = true;
                //update the db here
            }
            catch (DbUpdateConcurrencyException)
            {
                return new NotFoundResult();
            }

            return new StatusCodeResult(200);
        }
        else
        {
            return BadRequest(ModelState.Values.SelectMany(v => v.Errors).Select(error => error.ErrorMessage));
        }
    }
}

谢谢

1 个答案:

答案 0 :(得分:0)

在控制器示例中公开控制器方法PutActivity的URL是PUT api/scheduler/{id}

要使用该Update方法访问该URL。

.Update(update => update.Action("Put", "Scheduler", new { id = "{0}" }))

See this demo as example

或者

如果要使用URL api/Scheduler/PutActivity/{id}(与GET相似的模式),则需要按如下所示通过put方法修改属性。

[HttpPut("PutActivity/{id}")]
public IActionResult PutActivity(int id, MeetingViewModel task)

然后,您可以通过此asp.net操作调用来调用api/Scheduler/PutActivity/{id}

.Update(update => update.Action("PutActivity", "Scheduler", new { id = "{0}" }).Type(HttpVerbs.Put))