gpt4 book ai didi

jquery - ASP.Net MVC 3 JQGrid

转载 作者:行者123 更新时间:2023-12-03 22:36:05 25 4
gpt4 key购买 nike

阅读完 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'
});
});

<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>

然后在我的 Controller 中,我有以下方法应该返回 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);

}

当我运行此代码时,代码失败并出现以下错误

LINQ to Entities does not recognize the method 'System.String ToString()' method

有谁知道如何修复这个错误吗?而且,我是否以正确的方式执行此操作,或者我应该以与 Phil Haack 解释不同的方式执行此操作,因为他使用的是 Linq to SQL?

如有任何反馈,我们将不胜感激。

谢谢大家。

最佳答案

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);

}

关于jquery - ASP.Net MVC 3 JQGrid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5092866/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com