2012-01-30 44 views
2

我必須使用jQuery網格在mvc3中加載數據。但我無法擺脫這個錯誤。在jQuery網格中解析上下文變量和模式時出錯

enter image description here

Layout.cshtml

<link type="text/css" href="@Url.Content("~/Content/jquery-ui-titoms.css")" rel="stylesheet" /> 
<link type="text/css" href="@Url.Content("~/Content/ui.jqgrid.css")" rel="stylesheet" /> 

<script type="text/javascript" src="@Url.Content("~/Scripts/jquery-1.7.1.min.js")"></script> 
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery-ui-1.8.16.custom.min.js")"></script> 
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery-ui-timepicker-addon.js")"></script> 
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery-ui-sliderAccess.js")"></script> 

<script type="text/javascript" src="@Url.Content("~/Scripts/grid.locale-en.js")"></script> 
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery.jqGrid.min.js")"></script> 

Index.cshtml

<script type="text/javascript"> 
    jQuery(document).ready(function() { 
     jQuery("#list").jqGrid({ 
      url: '@Url.Content("~/Device/LoadDevice")', 
      datatype: 'json', 
      mtype: 'POST', 
      colNames: ['LoginID', 'Name', 'Model'], 
      colModel: [ 
      { name: 'LoginID', index: 'LoginID', width: 40, align: 'left' }, 
      { name: 'Name', index: 'Name', width: 40, align: 'Name' }, 
      { name: 'Model', index: 'Model', width: 400, align: 'Model'}], 
      pager: jQuery('#pager'), 
      rowNum: 10, 
      rowList: [5, 10, 20, 50], 
      sortname: 'Name', 
      sortorder: "desc", 
      viewrecords: true, 
      imgpath: '', 
      caption: 'Device list' 
     }); 
    }); 
</script> 
<table id="list" class="scroll" cellpadding="0" cellspacing="0"></table> 
<div id="pager" class="scroll" style="text-align:center;"></div> 

DeviceController

public JsonResult LoadDevice(string sidx, string sord, int page, int rows) 
{ 
    using (Dbase titoms = new Dbase()) 
    { 
     var context = titoms; 
     int pageIndex = Convert.ToInt32(page) - 1; 
     int pageSize = rows; 
     int totalRecords = context.devices.Count(); 
     int totalPages = (int)Math.Ceiling((float)totalRecords/(float)pageSize); 

     var devices = context.devices.OrderBy(sidx + " " + sord).Skip(pageIndex * pageSize).Take(pageSize); 

     var sorted = (from item in devices 
         select new 
         { 
          i = item.DeviceID, 
          cell = new string[] { item.LoginID, item.Name, item.Model } 
         }).ToArray(); 

     var jsonData = new 
     { 
      total = totalPages, 
      page, 
      records = totalRecords, 
      rows = sorted 
     }; 
     return Json(jsonData); 
    } 
}  

這裏是頁的預覽:

enter image description here

LoginIDName & Model在類型string同時的DeviceID是long類型。
我正在使用實體框架。

現在我找不到爲什麼這樣的錯誤存在,希望有人可以幫助..非常感謝。

編輯

感謝尼古拉,你的答案工作,但我不得不改變一些代碼。這應該現在工作:

 var devices = context.devices.OrderBy("it." + sidx + " " + sord).Skip(pageIndex * pageSize).Take(pageSize); 

     var sorted = (from item in devices 
         select new 
         { 
          i = item.DeviceID, 
          cell = new List<string> { item.LoginID, item.Name, item.Model } 
         }).ToArray(); 

回答

2

問題是由於在運行時按標準指定您的訂單。

你需要去改變它遵循convention

ObjectQuery<Product> productQuery2 = 
    productQuery1.OrderBy("it.ProductID"); 

所以,你的查詢會是這樣

var devices = context.devices.OrderBy("it." + sidx) 

看到其他question

+0

+1你是高手。謝謝!!!! – fiberOptics 2012-01-30 10:53:36

+0

很高興幫助 - 雖然不知道主人的事情... – 2012-01-30 11:09:47