2014-05-24 4 views
0

jqgrid에서 데이터베이스에 연결하려고합니다. 컨트롤러에이 버그가 있습니다. 누구든지 문제를 해결할 방법을 알고 있습니까? 데이터를 제공하는 경우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에 편집을 추가하는 방법을 묻고 싶습니다.

보기

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

나는 생각 때문에 momen에 대한 질문 당신은이를 보낼 수 없습니다

답변

0

,이 데이터베이스로 전송됩니다 T :

question.CatID.ToString() 

SQL Server로 메소드 호출로 무엇을 해야할지하지 않기 때문에. 집합의 열거를 강제로 수행하면 메모리에 .NET 메소드를 사용할 수 있습니다.

from question in context.Cats.ToList() 

성능 문제가 해결 될 것입니다. Cats 필터되지 않은 세트가 큰 테이블은 메모리로로드 될 때 성능이 저하됩니다. 귀하의 경우에는 나중에 어쨌든 ToArray()을 호출 할 때 차이점을 느끼지 못할 것입니다.

가능하면 페이징을 구현하고 세트를 작게 유지하려면 Skip()Take()을 사용하십시오.

컨텍스트를 두 번 누르지 말고 rowsrecords 속성을 참조 할 수 있도록 결과를 변수에 저장하는 것도 고려해야합니다.

+0

고맙습니다. 작동합니다. 문안 인사 – user3128303

관련 문제