Kendo Grid无法填充视图

时间:2014-06-11 21:55:11

标签: c# asp.net-mvc lambda kendo-grid

我正在尝试使用数据库中的数据填充kendo ui网格。我找不到具有相同问题的所有其他帖子的答案。用户使用UserId和To / From Dates进行搜索。使用断点我可以看到通过控制器返回的数据。第一次使用剑道,所以如果我需要发布更多信息,请告诉我

错误消息

Compiler Error Message: CS1660: Cannot convert lambda expression to type 'string' because it is not a delegate type

Source Error:

Line 11:     .Columns(columns =>
Line 12:     {
Line 13:         columns.Bound(m => m.ActivityDate).Format("{0:MM/dd/yyyy}");
Line 14:         columns.Bound(m => m.Assignment);
Line 15:         columns.Bound(m => m.Action);

控制器

[HttpPost]
public ActionResult ReportsPhoneSupport(ReportsPhoneSupport model)
{       
    string[] userIds = model.UserId.Split(',');

    foreach (string userId in userIds)
    {
        int iUserId = 0;
        if (Int32.TryParse(userId, out iUserId))
        {
            ReportPhoneSupportResultTypedView results = new ReportPhoneSupportResultTypedView();
            RetrievalProcedures.FetchReportPhoneSupportResultTypedView(results, model.FromDate, model.ToDate, iUserId);
        }
    }

    return View(model);
}

查看

@{
    ViewBag.Title = "ReportsPhoneSupport";
 }

<h2>ReportsPhoneSupport</h2>

@(Html.Kendo().Grid<UtilityWebSite.Controllers.ReportsPhoneSupportController>()
    .Name("grid")
    .Columns(columns =>
    {
        columns.Bound(m => m.ActivityDate).Format("{0:MM/dd/yyyy}");
        columns.Bound(m => m.Assignment);
        columns.Bound(m => m.Action);
        columns.Bound(m => m.ToFrom);
        columns.Bound(m => m.Result);
        columns.Bound(m => m.Description);
    })
    .Pageable()
    .Sortable()
    .Scrollable()
    .Filterable()
    .HtmlAttributes(new { style = "height:430px;" })
    .DataSource(dataSource => dataSource
        .Ajax()
        .PageSize(20)
        .Read(read => read.Action("ReportsPhoneSupport", "ReportsPhoneSupport"))
    )
)

CLASS

 public class ReportsPhoneSupport
{
    public string UserId { get; set; }
    public DateTime ToDate { get; set; }
    public DateTime FromDate { get; set; }

}

} Data From SQL

1 个答案:

答案 0 :(得分:1)

你需要将Json返回到网格而不是视图。

类似

Public jsonresult readData([DataSourceRequest] DataSourceRequest request, MyModel model)
{
 Do something here to get data for source
 Eg List<MyReturnedModel> returnModel = new List<MyReturnedModel>()
 { new MyReturnedModel(){ id = 1}, (etc)
  };

Return json(returnedModel.ToDataSourceResult(request,ModelState), jsonbehaviour.Get);
}

希望这会给你足够的想法来运行。

编辑:

我还注意到,当它应该是您的模型类时,您将控制器类设置为网格实体。

第二次编辑:

好吧如果我有这个权利,网格上显示的实际数据应该是 ReportPhoneSupportResultTypedView

所以如果你先将网格更改为:

@(Html.Kendo().Grid<**ReportPhoneSupportResultTypedView**>()
 (include any namespaces here for the model type to be referenced correctly.)
 .Name("grid")
.Columns(columns =>
{
    columns.Bound(m => m.ActivityDate).Format("{0:MM/dd/yyyy}");
    columns.Bound(m => m.Assignment);
    columns.Bound(m => m.Action);
    columns.Bound(m => m.ToFrom);
    columns.Bound(m => m.Result);
    columns.Bound(m => m.Description);
})
.Pageable()
.Sortable()
.Scrollable()
.Filterable()
.HtmlAttributes(new { style = "height:430px;" })
.DataSource(dataSource => dataSource
    .Ajax()
    .PageSize(20)
    .ServerOperations(true)
   .Read(read => read.Action("ReportsPhoneSupport", "ReportsPhoneSupport").Data("MyFilters"))
)

您会注意到我在您的阅读操作后添加了.Data并使用了一些javascript您可以添加搜索参数

这样的事情:

<script>
function MyFilters()
{
     return { filterModel: {   
                               UserId: 1,
                               ToDate: "01 Jan 2014",
                               FromDate: "20 Jan 2014"
                            }
            };
}
</script>

然后,这应该构建您传回的模型对象以执行过滤,然后将您的读取操作更改为以下内容:

注意确保将名称空间Kendo.Mvc.Extensions和Kendo.Mvc.UI包含到控制器中,以便正确选取DataSourceRequest / toDataSourceResult。

public JsonResult ReportsPhoneSupport([DataSourceRequest] DataSourceRequest request,ReportsPhoneSupport filterModel )
{       
   string[] userIds = filterModel.UserId.Split(',');

foreach (string userId in userIds)
{
    int iUserId = 0;
    if (Int32.TryParse(userId, out iUserId))
    {
        ReportPhoneSupportResultTypedView results = new ReportPhoneSupportResultTypedView();
        RetrievalProcedures.FetchReportPhoneSupportResultTypedView(results, filterModel.FromDate, filterModel.ToDate, iUserId);
    }
}
  **I am assuming your results set is some form of enumerable/IList**
return Json( results.toDataSourceResult(request,ModelState), jsonbehaviour.AllowGet);

}

希望这个扩展示例能够为您提供所需的答案。