JQGrid在ASP.Net MVC与VB.Net [英] JQGrid on ASP.Net MVC with VB.Net

查看:136
本文介绍了JQGrid在ASP.Net MVC与VB.Net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一个简单的表创建一个JQGrid。









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



我将其修改为我自己的数据库,并提出了这个功能公共函数SelectGridData(ByVal sidx As String,ByVal sord As String,ByVal page As Integer,ByVal rows As Integer)作为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用{_
.total = totalPages,_
.page = page,_
.records = totalRecords,_
.rows =(From p In context.Issues _
Order By(p.ID& & sord)_
用{.id = p.ID,.cell = _
{p.ID,p.Image_Path,p.Magazine_Type,p.Magazine_Path}}选择新建ToArray()}

返回Json(jsonData,JsonRequestBehavior.AllowGet)
结束函数

网格确实显示没有任何数据,系统引发错误



错误描述说



无法将类型System.Int32转换为System.Object类型,LINQ to Entities只支持转换Entity Data Model原始类型。



任何帮助赞赏,如果这是由于一些基本的误会,请指导我,我愿意做一些努力。



谢谢



编辑:最终根据Oleg的建议工作的代码

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

Dim My_Array As Array
Dim My_Se cond_Array As Array

Simple_Object = From p In Context.Issues _
Order By(p.ID& & sord)_
选择新的{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)_
选择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
如果i> 0然后
ReDim保存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}
下一个

类My_Record_Type
公共ID作为整数
公共单元格作为字符串()
结束类


解决方案

您尝试填写 / code>属性一步。问题是您使用Entity Framework在数据类型的转换中有一些限制。您可以解决问题,如果您首先进行查询以获取您不需要任何数据转换所需的项目,并将项目的中间结果保存为列表。之后,您可以创建另一个LINQ查询,您将 p.ID 的显式转换包含到字符串中。此外,您当前的代码不会使用任何数据分页。因此,如果您不使用 loadonce:true ,则用户将永远不会看到更多的页面。



它是我很难在VB中编写正确的代码而不进行调试。自从去年以来,我使用C#。我建议您查看答案了解更多信息。它包含服务器端过滤的实现。如果您不需要它,并删除相应的代码,其余代码将很短,我希望这将很容易理解。


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

解决方案

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.

这篇关于JQGrid在ASP.Net MVC与VB.Net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆