Asp.Net MVC EasyUI DataGrid查询分页
function doSearch() { //查询方法 var searchValue = $('#txtQueryTC001').textbox('getText'); $('#dgCMSTC').datagrid('options').queryParams = { condition: searchValue }; //initDataGrid(searchValue); $.ajax({ type: 'post', url: '/CMSTC/GetJson', data: 'condition=' + searchValue, dataType: 'json', error: function (XMLHttpRequest, textStatus, errorThrown) { $.messager.alert("查询数据", "发生错误!" + errorThrown, "error"); }, success: function (data) { console.info(data); //$.messager.alert('数据', JSON.stringify(data), 'info'); $('#dgCMSTC').datagrid('load', []); $('#dgCMSTC').datagrid('load', data); //initDataGrid(searchValue); } }); } $(function () { //初始化datagrid $('#dgCMSTC').datagrid({ method: 'post', url: '/CMSTC/GetJson', singleSelect: true, fit: true, border: true, pagination: true, pageSize: 20, queryParams: { condition: '' }, columns: [[ { field: 'TC001', title: '仓库编号', width: 100 }, { field: 'TC002', title: '仓库名称', width: 100 }, { field: 'TC003', title: '仓库电话', width: 100 }, { field: 'CREATOR', title: '创建者', width: 100 }, { field: 'CREATE_DATE', title: '创建日期', width: 100 }, { field: 'MODIFIER', title: '修改者', width: 100 }, { field: 'MODI_DATE', title: '修改日期', width: 100 } ]], toolbar: '#toolQuery', onLoadSuccess: function (data) { if (data.total > 0) { $('#dgCMSTC').datagrid('selectRow', 0); } } }); }); <a id="btnSearch" href="#" class="easyui-linkbutton" iconcls="icon-search" plain="true" onclick="doSearch();">Search</a>
后台代码:
public JsonResult GetJson() { using (var myDb = new studydb<CMSTC>(strConn)) { string strCondition = Request.Form["condition"]; int page = 1; int rows = 20; List<CMSTC> myCMSTC; List<CMSTC> myCMSTCPAGE; if (Request.Form["page"]!=null) { page = Convert.ToInt32(Request.Form["page"].ToString()); } if (Request.Form["rows"] != null) { rows = Convert.ToInt32(Request.Form["rows"].ToString()); } if (string.IsNullOrEmpty(strCondition)) { myCMSTC = myDb.CMSTC.ToList(); //查询数据都是正确的 myCMSTCPAGE = myDb.CMSTC.OrderBy(i => i.TC001).Skip((page - 1) * rows).Take(rows).ToList(); } else { myCMSTC = myDb.CMSTC.Where(p => p.TC001.Contains(strCondition) || p.TC002.Contains(strCondition)).ToList(); //查询数据都是正确的 myCMSTCPAGE = myDb.CMSTC.OrderBy(i => i.TC001).Where(p => p.TC001.Contains(strCondition) || p.TC002.Contains(strCondition)).Skip((page - 1) * rows).Take(rows).ToList(); } return Json(new { total = myCMSTC.Count, rows = myCMSTCPAGE }, JsonRequestBehavior.AllowGet); } }
我在webform里都是这样写的,但是换到mvc里就有问题了,主要是查询完了加载本地数据的地候,主要是这句:
$('#dgCMSTC').datagrid('load', []);
$('#dgCMSTC').datagrid('load', data);