【问题标题】:Error during database connection with jqgrid与 jqgrid 连接数据库时出错
【发布时间】:2014-05-24 00:35:40
【问题描述】:

我正在尝试从 jqgrid 连接到数据库。我在控制器中有这个错误,有人知道如何修复它吗?

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

另外想问一下jqgrid怎么添加edit?

查看

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

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

【问题讨论】:

    标签: asp.net-mvc linq jqgrid jqgrid-asp.net


    【解决方案1】:

    你不能发送这个:

    question.CatID.ToString()
    

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

    from question in context.Cats.ToList()
    

    问题在于平衡性能。如果Cats unfiltered set 很大,则整个表都加载到内存中时性能会受到影响。在您的情况下,您可能不会注意到差异,因为您稍后会调用ToArray()

    如果可能,请使用Skip()Take() 来实现分页并保持您的集合较小。

    您还应该考虑将结果存储在一个变量中,以便您可以为您的rowsrecords 属性引用它,而不是两次点击上下文。

    【讨论】:

      猜你喜欢
      • 2021-01-22
      • 2019-01-12
      • 1970-01-01
      • 2015-07-02
      • 2015-07-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多