tco*_*ode 7 jquery jqgrid jqgrid-asp.net asp.net-mvc-3
在阅读了JQGrid控件之后,我决定在我的一个ASP.Net MVC 3 Web应用程序中使用它.
首先,我按照Phil Haacks教程http://haacked.com/archive/2009/04/14/using-jquery-grid-with-asp.net-mvc.aspx这一切都很好.然后我尝试在我的应用程序中实现类似的东西,唯一的区别是,我使用Linq To Entities.
我的View页面已经导入了所有的css和Jquery类,然后我有我的JavaScript函数和保存数据的表
<script type="text/javascript">
jQuery(document).ready(function () {
jQuery("#list").jqGrid({
url: '/Home/LinqGridData/',
datatype: 'json',
mtype: 'GET',
colNames: ['equipmentID', 'categoryTitle', 'title'],
colModel: [
{ name: 'equipmentID', index: 'equipmentID', width: 40, align: 'left' },
{ name: 'categoryTitle', index: 'categoryTitle', width: 40, align: 'left' },
{ name: 'title', index: 'title', width: 200, align: 'left'}],
pager: jQuery('#pager'),
width: 660,
height: 'auto',
rowNum: 10,
rowList: [5, 10, 20, 50],
sortname: 'Id',
sortorder: "desc",
viewrecords: true,
imgpath: '/scripts/themes/coffee/images',
caption: 'My first grid'
});
});
Run Code Online (Sandbox Code Playgroud)
<h2>My Grid Data</h2>
<table id="list" class="scroll" cellpadding="0" cellspacing="0"></table>
<div id="pager" class="scroll" style="text-align:center;"></div>
Run Code Online (Sandbox Code Playgroud)
然后在我的控制器中,我有以下方法,假设返回Json数据
public ActionResult LinqGridData(string sidx, string sord, int page, int rows)
{
AssetEntities context = new AssetEntities();
var query = from e in context.Equipments
select e;
var count = query.Count();
var result = new
{
total = 1,
page = page,
records = count,
rows = (from e in query
select new
{
id = e.equipmentID,
cell = new string[]
{
e.equipmentID.ToString(),
e.Category.categoryTitle,
e.Department.title
}
}).ToArray()
};
return Json(result, JsonRequestBehavior.AllowGet);
}
Run Code Online (Sandbox Code Playgroud)
当我运行它时,代码会因以下错误而崩溃
LINQ to Entities does not recognize the method 'System.String ToString()' method
Run Code Online (Sandbox Code Playgroud)
有谁知道如何解决这个错误?而且,我是以正确的方式做到这一点,还是我应该采用与Phil Haack解释不同的方式,因为他使用Linq到SQL?
任何反馈都将非常感激.
谢谢伙计们.
Kim*_*jan 14
EF不支持ToString方法,必须在没有ToString和格式的情况下检索数据
这应该工作
public ActionResult LinqGridData(string sidx, string sord, int page, int rows)
{
AssetEntities context = new AssetEntities();
var query = from e in context.Equipments
select e;
var count = query.Count();
var result = new
{
total = 1,
page = page,
records = count,
rows = query.Select(x => new { x.equipamentID, x.Category.categoryTitle,x.Department.title })
.ToList() // .AsEnumerable() whatever
.Select(x => new {
id = x.equipamentID,
cell = new string[] {
x.equipamentID.ToString(),
x.categoryTitle,
x.title
}})
.ToArray(),
};
return Json(result, JsonRequestBehavior.AllowGet);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
22947 次 |
| 最近记录: |