0

我在剑道网格中显示数据时遇到了一些问题。我的场景是这样的:我有一个网格,应该显示有关某个项目的任务的信息。只有与该项目相关的任务才应显示在网格上。我正在使用 ajax 绑定从服务器获取数据。但是,网格不显示任何数据。我相当确定 Read 函数正在返回正确的结果,并且我认为格式正确,但显然我错了。下面是我的代码。

这是我的模型定义:

Public Class TaskViewModel
    Public Property IdTasks As Integer

    Public Property Task As String
    Public Property AssignedToId As Nullable(Of Integer)
    Public Property StartDate As Nullable(Of Date)
    Public Property DueDate As Nullable(Of Date)

    Public Property usersAssignedTo As UserViewModel
    Public Property costChange As CostChangeViewModel
End Class

Public Class CostChangeViewModel
     Public Property Text as String
     Public Property Value as Integer
End Class

Public Class UserViewModel
     Public Property Text as String
     Public Property Value as Integer
End Class

从控制器读取动作

Function Tasks_Read(familyId As Integer) As JsonResult
        'get the customer family that is being viewed on the margin report
        Dim custFamily As CustomerFamilies = (From a In dbLMT.CustomerFamilies Where a.CustomerFamilyId = familyId).Single
        'get a list of tasks related to that customer family
        Dim lstTasks As IQueryable(Of Tasks) = getTaskList(custFamily)
        'create a queryable of the task view model and select only those properties
        Dim lstTaskViewModel As IQueryable(Of TaskViewModel) = (From a In lstTasks Select New TaskViewModel With {.IdTasks = a.IdTasks, .Task = a.Task, .AssignedToId = a.AssignedToId, .usersAssignedTo = New UserViewModel With {.Text = a.usersAssignedTo.FirstName & " " & a.usersAssignedTo.LastName, .Value = a.usersAssignedTo.IdUsers}, .StartDate = a.StartDate, .DueDate = a.DueDate, .costChange = New CostChangeViewModel With {.Text = a.costChange.CostChangeDesc, .Value = a.costChange.CostChangeProjectId}})

        Return Json(lstTaskViewModel, JsonRequestBehavior.AllowGet)
End Function

上面的函数似乎工作正常,并且 lstTaskViewModel 包含具有正确属性的正确任务。

我的网格代码如下所示:

@Html.Kendo.Grid(Of TaskViewModel).Name("gridTasks").Columns(Sub(col)
                                                                             col.Bound(Function(p) p.IdTasks).Title("Id")
                                                                             col.Bound(Function(p) p.costChange).Title("Cost Change Project").ClientTemplate("#=costChange.Text#").Width(200)
                                                                             col.Bound(Function(p) p.Task).Title("Task").Width(400)
                                                                             col.Bound(Function(p) p.StartDate).Title("Start Date").Width(100).Format("{0:MM/dd/yyyy}")
                                                                             col.Bound(Function(p) p.DueDate).Title("Due Date").Width(100).Format("{0:MM/dd/yyyy}")
                                                                             col.Bound(Function(p) p.usersAssignedTo).Title("Assigned To").ClientTemplate("#=usersAssignedTo.Text#").Width(100)
                                                                         End Sub).DataSource(Sub(ds)
                                                                                                     ds.Ajax().Read(Sub(rd)
                                                                                                                            rd.Action("Tasks_Read", "Reports").Data("getFamilyId")
                                                                                                                    End Sub).Model(Sub(model)
                                                                                                                                           model.Id(Function(p) p.IdTasks)
                                                                                                                                   End Sub)
                                                                                             End Sub).AutoBind(True)

使用上面的网格,我成功地创建了新任务并显示了它们,但是我看不到从Tasks_Read传递的任务列表。(出于测试目的,我已取出所有未读取数据的内容)

使用 IE 调试器,我得到了Tasks_Read返回到网格的 JSON 字符串:

[{"IdTasks":7027,"Task":"this is a test to ensure that creating a task and an issue is possible","AssignedToId":3151,"StartDate":"\/Date(1438056000000)\/","DueDate":"\/Date(1438228800000)\/","usersAssignedTo":{"Text":"Eric Hemphill","Value":3151},"costChange":{"Text":"Change Final Assy AT from 0.82 to 0.73","Value":125}}]

在我看来,JSON 结果似乎适合网格中的模型,但网格没有显示任何数据。我想我已经阅读了所有有类似问题的帖子,但到目前为止没有任何效果。

欢迎任何想法。谢谢您的帮助!

4

1 回答 1

1

I figured it out after many more hours of trying. In the read function of the controller, there needs to be a Kendo.mvc.ui.dataSourceRequest object that gets passed in. This object is sent from the grid. Then, before the Enumerable is returned, it needs to be converted to a Kendo DataSourceResult and then to a JSON object. (I think my use of the word object is wrong, so don't write any papers on it.) Anyway, the controller function should look something like this:

Function Tasks_Read(dataRequest as Kendo.Mvc.UI.DataSourceRequest, other param) as JsonResult {
'get your list of tasks and store them in some sort of enumerable
dim lstTasks as IQueryable(of Tasks) = (Get your list)


'convert the enumerable to a dataSourceResult
dim dataResult as Kendo.Mvc.UI.DataSourceResult = lstTasks.ToDataSourceResult(dataRequest)

To use the ToDataSourceResult function, you need to import the kendo.mvc.extensions Namespace.

Everything in this post is in their documentation, so it's nothing crazy, I misunderstood their docs. I hope this helps someone in the future. Good Luck All!

于 2015-07-30T17:53:08.973 回答