2012-03-29 39 views
-1

我想爲一個簡單的表JQGrid。從JQGrid與ASP.Net MVC與VB.Net

http://haacked.com/archive/2009/04/14/using-jquery-grid-with-asp.net-mvc.aspx

後用VB下通過翻譯版本從

http://www.qa.downappz.com/questions/jqgrid-sorting-in-vb-net-mvc-app.html

我把它修改爲我自己的數據庫,並使用此功能

Public Function SelectGridData(ByVal sidx As String, ByVal sord As String, ByVal page As Integer, ByVal rows As Integer) As ActionResult 
    Dim context As New IssueDBEntities 
    Dim pageIndex As Integer = Convert.ToInt32(page) - 1 
    Dim pageSize As Integer = rows 
    Dim totalRecords As Integer = context.Issues.Count() 
    Dim totalPages As Integer = CInt(Math.Ceiling(CSng(totalRecords)/CSng(pageSize))) 

    Dim jsonData = New With { _ 
    .total = totalPages, _ 
    .page = page, _ 
    .records = totalRecords, _ 
    .rows = (From p In context.Issues _ 
     Order By (p.ID & " " & sord) _ 
     Select New With {.id = p.ID, .cell = _ 
        {p.ID, p.Image_Path, p.Magazine_Type,p.Magazine_Path}}).ToArray()} 

    Return Json(jsonData, JsonRequestBehavior.AllowGet) 
End Function 
上來

網格確實顯示沒有任何數據,系統將引發錯誤

錯誤描述說

「無法投類型‘System.Int32’輸入‘System.Object的’。 LINQ到實體僅支持鑄造實體數據模型的基本類型。」

任何幫助表示讚賞,如果這是由於一些基本的誤解,請指導我,我願意做一些艱苦的工作。

謝謝

編輯:終於當過每奧列格的建議代碼

Dim Simple_Object As IQueryable(Of Object) 
     Dim Second_Simple_Object As IQueryable(Of Object) 

     Dim My_Array As Array 
     Dim My_Second_Array As Array 

     Simple_Object = From p In Context.Issues _ 
        Order By (p.ID & " " & sord) _ 
       Select New With {p.ID, p.Image_Path, p.Magazine_Type, p.Magazine_Path} 

     My_Array = Simple_Object.ToArray() 

     Second_Simple_Object = From p In Context.Issues _ 
        Order By (p.ID & " " & sord) _ 
       Select New With {p.ID} 

     My_Second_Array = Second_Simple_Object.ToArray() 

     Dim My_Result(0) As My_Record_Type 

     For i = 0 To My_Array.GetLength(0) - 1 
      If i > 0 Then 
       ReDim Preserve My_Result(i) 
      End If 

      My_Result(i) = New My_Record_Type 

      My_Result(i).id = CInt(My_Second_Array(i).ID) 
      My_Result(i).Cell = {My_Array(i).ID.ToString, My_Array(i).Image_Path.ToString, _ 
            My_Array(i).Magazine_Type.ToString, My_Array(i).Magazine_Path.ToString} 
     Next 

Class My_Record_Type 
    Public id As Integer 
    Public Cell As String() 
End Class 

回答

2

您嘗試填寫rows財產一步的問題是,你採用E。 ntity Framework在轉換數據類型時有一些限制。您可以解決問題,如果您首先進行查詢獲取您需要的項目沒有任何數據轉換並保存中間結果爲List的項目。之後,您可以創建另一個LINQ查詢,其中包含明確轉換爲p.ID的字符串。而且你現在的代碼不使用任何數據分頁。因此,如果您不使用loadonce: true,用戶將永遠不會看到更多的第一頁。

我很難在沒有調試的情況下在VB中編寫正確的代碼。我從去年開始就使用C#。我建議你看看the answer瞭解更多信息。它包含了服務器端過濾的實現。如果你不需要它,並刪除相應的代碼,其餘代碼將非常短,我希望它很容易理解。

+0

謝謝你的回覆是的,我通過使它們分開 – skv 2012-04-02 04:04:44

+0

謝謝試過您的解決方案你的答覆,是的我試圖通過使他們分開代碼的解決方案,我不得不創建一個對象來實現這個結果,和JSON似乎罰款與填充數據,但不知何故我的網頁沒有顯示,但我認爲你的建議解決了原來的問題,所以非常感謝你 – skv 2012-04-02 04:15:13

+0

我已經發布了代碼,在其他論壇上,我問了這個問題,因爲我不能在這裏發表http://forums.asp.net/p/ 1786677/4910852.aspx/1?p = True&t = 634689228293174596 – skv 2012-04-02 04:33:40