簡體   English   中英

Kendo Grid Ajax綁定未顯示數據

[英]Kendo Grid Ajax Binding Not showing Data

我在Kendo網格中顯示數據時遇到一些問題。 我的情況是這樣的:我有一個網格,該網格應顯示有關特定項目任務的信息。 只有與該項目相關的任務才應顯示在網格上。 我正在使用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)

. 使用上面的網格,我已經成功創建了新任務並顯示了它們,但是我看不到從傳遞來的任務列表。 (我已經刪除了所有未出於測試目的而讀取數據的內容)

使用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結果似乎適合網格中的模型,但是網格不顯示任何數據。 我認為我已經閱讀了所有存在類似問題的帖子,但到目前為止,都沒有任何效果。

任何想法都歡迎。 謝謝您的幫助!

經過數小時的嘗試,我弄清楚了。 在控制器的讀取功能中,需要有一個傳入的Kendo.mvc.ui.dataSourceRequest對象。該對象是從網格發送的。 然后,在返回Enumerable之前,需要將其轉換為Kendo DataSourceResult,然后轉換為JSON對象。 (我認為我對“對象”一詞的使用是錯誤的,因此不要在其上寫任何論文。)無論如何,控制器功能應如下所示:

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)

要使用ToDataSourceResult函數,您需要導入kendo.mvc.extensions命名空間。

這篇文章中的所有內容都在他們的文檔中,所以這並不瘋狂,我誤解了他們的文檔。 我希望這對以后的人有所幫助。 祝你好運!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM