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 good news is my grid shows up properly, however I get an error in this line
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()}
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.
Edit: After solving some case issues, my grid did work with this code
Hi Decker
Thank you for your reply, yes converting to an object, along with a suggestion from stackoverflow to split the entity framework part from the actual conversion helped solve the issue, but the grid isnt showing any data, but I guess thats a separate issue.
My Code in case if anyone needs it in future, I shall make an edit in case if there is an error in this part in making the grid work. My code is not very neat, if anyone can suggest improvements it would be great as well.
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
skvignesh
Contributor
2670 Points
512 Posts
JQGrid with VB.Net
Mar 29, 2012 07:37 AM|LINK
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 FunctionThe good news is my grid shows up properly, however I get an error in this line
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()}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.
S.K.Vignesh
ignatandrei
All-Star
137698 Points
22155 Posts
Moderator
MVP
Re: JQGrid with VB.Net
Mar 29, 2012 08:03 AM|LINK
skvignesh
Contributor
2670 Points
512 Posts
Re: JQGrid with VB.Net
Mar 29, 2012 08:22 AM|LINK
Hi ignatandrei
Thank you for your reply, I tried to change the code from
Order By (p.ID & " " & sord)
to
Order By p.ID
and also tried removing the entire line, however the error just remains.
S.K.Vignesh
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: JQGrid with VB.Net
Mar 30, 2012 11:39 PM|LINK
Hello:)
In my mind,I think you can do a cast convert by doing this to convert ID from a normal Int32 to object。
Select New With {.id = p.ID.ToString(), .cell = {p.ID.ToString(),……}……skvignesh
Contributor
2670 Points
512 Posts
Re: JQGrid with VB.Net
Apr 02, 2012 04:19 AM|LINK
Edit: After solving some case issues, my grid did work with this code
Hi Decker
Thank you for your reply, yes converting to an object, along with a suggestion from stackoverflow to split the entity framework part from the actual conversion helped solve the issue, but the grid isnt showing any data, but I guess thats a separate issue.
My Code in case if anyone needs it in future, I shall make an edit in case if there is an error in this part in making the grid work. My code is not very neat, if anyone can suggest improvements it would be great as well.
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} NextClass My_Record_Type Public id As Integer Public Cell As String() End ClassS.K.Vignesh