与jqgrid建立数据库连接时出错 [英] Error during database connection with jqgrid

查看:103
本文介绍了与jqgrid建立数据库连接时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从jqgrid连接到数据库.我的控制器中有这个错误,有人知道如何解决吗?

I'm trying to connect to database from jqgrid. I have this bug in the controller, does anyone know how to fix it?

Component LINQ to Entities does not recognize the method 'System.String ToString () "and you can not translate it to express the warehouse.

当给定的数据严格工作时. new {id = 1, cell = new[] {"1", "zzzzzz", "xxxxxx"}}

When given data rigidly works. new {id = 1, cell = new[] {"1", "zzzzzz", "xxxxxx"}}

此外,我想问一下如何向jqgrid添加编辑?

In addition, I would like to ask how to add edit to jqgrid?

查看

<asp:Content ID="indexTitle" ContentPlaceHolderID="TitleContent" runat="server">
    Home Page
</asp:Content>

<asp:content contentplaceholderid="HeadContent" runat="server">

    <link href="/Content/jquery-ui-1.8.7.css" rel="stylesheet" type="text/css" />
    <link href="/Content/ui.jqgrid.css" rel="stylesheet" type="text/css" />

    <script src="/Scripts/jquery-1.8.2.min.js" type="text/javascript"></script>
    <script src="/Scripts/js/i18n/grid.locale-en.js" type="text/javascript"></script>
    <script src="/Scripts/jquery.jqGrid.min.js" type="text/javascript"></script>

    <script type="text/javascript">
        $(function () {
            $("#list").jqGrid({
                url: '/Home/LinqGridData/',
                datatype: 'json',
                mtype: 'GET',
            colNames: ['CatID', 'CatName', 'Age'],
            colModel: [
            { name: 'CatID', index: 'CatID', width: 40, align: 'left' },
            { name: 'CatName', index: 'CatName', width: 40, editable: true, align: 'left' },
            { name: 'Age', index: 'Age', width: 400, align: 'left' }],
                pager: jQuery('#pager'),
                rowNum: 10,
                rowList: [5, 10, 20, 50],
                sortname: 'Id',
                sortorder: "desc",
                viewrecords: true,
                imgpath: '/scripts/themes/coffee/images',
                caption: 'My first grid'
            });
        jQuery("#list").jqGrid('navGrid', "#pager", { edit: true, add: true, del: true });
        jQuery("#list").jqGrid('inlineNav', "#pager");
        });
    </script>

</asp:content>

<asp:content contentplaceholderid="MainContent" runat="server">
    <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>

</asp:content>

模型

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace MvcApplication2.Models
{
    public class Cat
    {
        [Key]
        public int CatID { get; set; }
        public string CatName { get; set; }
        public string Age { get; set; }
    }
}

控制器

public ActionResult LinqGridData(string sidx, string sord, int page, int rows)
{
    var context = new CatEntities();

    var jsonData = new
    {
        total = 1, //todo: calculate
        page = page,
        records = context.Cats.Count(),
        rows = (
            from question in context.Cats
            select new
            {
                i = question.CatID,
                cell = new string[] { question.CatID.ToString(), question.CatName, question.Age }
            }).ToArray()
    };
    return Json(jsonData, JsonRequestBehavior.AllowGet);
}

我认为这是因为问题一时被发送到数据库了,

I think it's because the question for a moment it is sent to the database,

推荐答案

您无法发送此邮件:

question.CatID.ToString()

到SQL Server,因为它不知道如何处理方法调用.如果首先强制枚举集合,则可以在内存中使用.NET方法:

to SQL Server because it doesn't know what to do with the method call. If you force enumeration of the set first you can make use of .NET methods in memory:

from question in context.Cats.ToList()

问题将是平衡性能.如果Cats未过滤的设置较大,则将整个表加载到内存中会降低性能.就您而言,您以后再调用ToArray()时可能不会注意到有什么区别.

The problem is going to be balancing performance. If the Cats unfiltered set is large performance will suffer as the entire table is loaded into memory. In your case you probably won't notice a difference as you're invoking ToArray() later anyway.

如果可能的话,使用Skip()Take()来实现分页,并使集合变小.

If possible, use Skip() and Take() to implement paging and keep your sets small.

您还应该考虑将结果存储在变量中,以便您可以为rowsrecords属性引用它,而不必两次访问上下文.

You should also consider storing the result in a variable so that you can reference it for your rows and records properties rather than hitting the context twice.

这篇关于与jqgrid建立数据库连接时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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