ASP.Net MVC 3 JQGrid [英] ASP.Net MVC 3 JQGrid

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

问题描述

在阅读了 JQGrid 控件之后,我决定在我的一个 ASP.Net MVC 3 Web 应用程序中使用它会很好.

After reading up on the JQGrid control, I decided it would be good to use it in one of my ASP.Net MVC 3 Web applications.

首先我遵循了 Phil Haacks 教程 http:///haacked.com/archive/2009/04/14/using-jquery-grid-with-asp.net-mvc.aspx 这一切都很好.然后我尝试在我的应用中实现类似的东西,唯一的区别是,我使用 Linq To Entities.

Firstly I followed Phil Haacks tutorial http://haacked.com/archive/2009/04/14/using-jquery-grid-with-asp.net-mvc.aspx which is all good. I then tried to implement something similar into my app, the only difference being, I use Linq To Entities.

我的视图页面已导入所有 css 和 Jquery 类,然后我有我的 JavaScript 函数和保存数据的表

My View page has all the css and Jquery classes imported, then I have my JavaScript Function and table which holds the data

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

然后在我的控制器中,我有以下方法,假设返回 Json 数据

Then in my controller, I have the following method which is suppose to return the Json data

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

    }

当我运行它时,代码会出现以下错误

When I run this, the code falls over with the following error

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

有谁知道如何解决这个错误?而且,我这样做是正确的方式,还是应该以不同于 Phil Haack 的解释的方式来做,因为他使用的是 Linq to SQL?

Does anyone know how to fix this error? And also, am I doing this the correct way, or should I be doing it a different way from the Phil Haack explanation since he is using Linq to SQL?

任何反馈都将不胜感激.

Any feedback would be much appreciated.

谢谢大家.

推荐答案

EF不支持ToString方法,必须取回没有ToString和格式的数据

EF doesn't support ToString method, you must retrieve the data without ToString and format

这应该可以工作

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

}

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

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