从Kendo Grid调用我的服务时出现400错误请求

时间:2014-02-13 07:40:14

标签: c# javascript kendo-ui kendo-grid wcf-data-services

我正在我的asp.net mvc应用程序中处理网格,需要从现有服务接收对象列表。以前我已经填充了这个网格,在我的控制器中使用.net代码调用服务。但是,由于我正在进行的项目结构是chaning,我需要直接从网格中调用服务。我已经将我的实现从使用服务器包装器改为仅javascript;

 <div id="grid"></div>
            <script>
                $(document).ready(function() {
                    $("#grid").kendoGrid({
                        dataSource: {
                            type: "odata",
                            transport: {
                                read: {
                                    type: "GET",
                                    url: "http://idex-c1/idex/Unit/AlarmService.svc/GetAlarmsForUnit",
                                    data: {
                                        unitId: "1",
                                        fromDate: "",
                                        toDate: false
                                    }
                                }
                            },
                            schema: {
                                model: {
                                    fields: {
                                        UnitId: { type: "number" }
                                    }
                                }
                            },
                            pageSize: 20,
                            serverPaging: true,
                            serverFiltering: true,
                            serverSorting: true
                        },
                        height: 430,
                        filterable: true,
                        sortable: true,
                        pageable: true,
                        columns: [
                            {
                                field: "UnitId",
                                filterable: false
                            }
                        ]
                    });
                });
            </script>
        </div>

我的第一个问题是: 该服务通常返回具有多个属性的AlarmContracts列表。我的网格是否需要知道所有这些属性,或者我只能使用其中的一些(如本例中的ID)?

第二: 尝试拨打该服务时,为什么会收到400 - Bad请求? 就像我之前说过的那样,通过控制器中的.net代码调用服务工作;

public ActionResult Alarms(int id, DateTime? FromDate, DateTime? ToDate)
{
    var unit = UnitClient.GetUnit(id);

    var fromDate = FromDate ?? DateTime.Today.AddDays(-20);
    var toDate = ToDate ?? DateTime.Now;
    Model = new AlarmsViewModel
    {
        ViewUnitContract = UnitClient.GetUnit(id),
        Alarms = AlarmClient.GetAlarmsForUnit(unit.Name, fromDate, toDate)
                   .Where(x => x.DateOff == null || x.DateAck == null)
                   .ToArray(),
        UnitName = unit.Name,
        Unit = new UnitDetailsModel(unit),
        FromTime = fromDate,
        ToTime = toDate
    };

    return View(Model);
}

2 个答案:

答案 0 :(得分:2)

好的,所以我实际上设法找到了解决方案,毕竟使用包装器:) 这是我在cshtml中所做的:

   @(Html.Kendo().Grid(Model.Alarms)
                  .Name("grid")
                  .DataSource(dataSource => dataSource
                      .Ajax()
                      .ServerOperation(false)
                      .Model(m => m.Id(s => s.AlarmComment))
                      .Read(read => read.Action("Alarms_Read", "Alarms", new { id = Model.ViewUnitContract.Id }).Type(HttpVerbs.Get))
                      .AutoSync(true)
                  )
                  .Columns(col =>
                  {
                      col.Bound(p => p.DateOn).Format("{0:u}").Title("Date");
                      col.Bound(p => p.Priority).Width(50);
                      col.Bound(p => p.ExtendedProperty2).Width(100).Title("Action");
                      col.Bound(p => p.AlarmTag).Title("Name");
                      col.Bound(p => p.AlarmComment).Title("Comment");
                      col.Bound(p => p.ExtendedProperty1).Title("AlarmID");
                      col.Bound(x => x.DateOff).Title("Value");
                  })
                  .HtmlAttributes(new {style = "height:430px;"})

                  )

这是我的控制器动作:

[HttpGet]
public ActionResult Alarms_Read([DataSourceRequest] DataSourceRequest request, int id, DateTime? startDate, DateTime? endDateFilter)
{
    var unit = UnitClient.GetUnit(id);

    var fromDate = startDate ?? DateTime.Today.AddDays(-20);
    var toDate = endDateFilter ?? DateTime.Now;
    Model = new AlarmsViewModel
    {
        ViewUnitContract = UnitClient.GetUnit(id),
        Alarms = AlarmClient.GetAlarmsForUnit(unit.Name, fromDate, toDate)
            .Where(x => x.DateOff == null || x.DateAck == null)
            .ToArray(),
        UnitName = unit.Name,
        Unit = new UnitDetailsModel(unit),
        FromTime = fromDate,
        ToTime = toDate
    };

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

希望对其他人有帮助:)

答案 1 :(得分:-2)

你也可以考虑使用javascript库Datajs(http://datajs.codeplex.com/)来消费odata