-1

I am trying to make a JQGrid for a simple table.

After following through with a VB translated version from

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

from

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

I modified it to my own database and came up with this function

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

The grid does show up without any data, and the system throws an error

The error description says

"Unable to cast the type 'System.Int32' to type 'System.Object'. LINQ to Entities only supports casting Entity Data Model primitive types."

Any help is appreciated, if this is due to some basic misunderstanding, please guide me, I am willing to do some hard work.

Thank you

Edit: The code that finally worked as per Oleg's Suggestion

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
skv
  • 1,793
  • 3
  • 19
  • 27

2 Answers2

2

Here is complete sample of JQgrid with ASP.NET MVC 3 + C# (you can convert it to vb.net) http://www.dotnetacademy.blogspot.in/2012/04/using-jqgrid-in-aspnet-mvc-3.html

Dr. Rajesh Rolen
  • 14,029
  • 41
  • 106
  • 178
2

You try to fill rows property in one step. The problem is that you use Entity Framework which have some restrictions in the conversion of the data types. You can solve the problem if you first make query to get the items which you need without any data conversion and save intermediate results as List of items. After that you can make another LINQ Query where you include explicit conversion of p.ID to string. Moreover your current code don't use any paging of data. So the user will never seen more as the first page if you don't use loadonce: true.

It's difficult for me to write correct code in VB without debugging it. I use C# instead since last years. I recommend you to look at the answer for more information. It contains implementation of the server side filtering. If you don't need it and remove the corresponding code the rest code will be very short and I hope it will be easy to understand.

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Thank you for the reply and yes I tried your solution by making them separate – skv Apr 02 '12 at 04:04
  • Thank you for the reply and yes I tried your solution by making them separate the code, I had to create an object to achieve this result, and the json seems fine with the data filled in, but somehow my page isnt showing up, but I think your suggestion solved the original issue so thank you very much – skv Apr 02 '12 at 04:15
  • I have posted the code that worked in the other forum where I asked the question as I am not able to post the same here http://forums.asp.net/p/1786677/4910852.aspx/1?p=True&t=634689228293174596 – skv Apr 02 '12 at 04:33
  • Sorry for multiple comments, I am writing as things happen, the code in the above page works and my grid populates fine... I am so happy that it worked finally @Oleg thanks once again – skv Apr 02 '12 at 04:50
  • @skv: You are welcome! It's a good news! You can any time modify your *original* question and append the text with additional information. Best wishes! – Oleg Apr 02 '12 at 05:02