jquery - ASP.Net MVC 3 JQGrid -
after reading on jqgrid control, decided use in 1 of asp.net mvc 3 web applications.
firstly followed phil haacks tutorial http://haacked.com/archive/2009/04/14/using-jquery-grid-with-asp.net-mvc.aspx good. tried implement similar app, difference being, use linq entities.
my view page has css , jquery classes imported, have javascript function , table holds data
<script type="text/javascript"> jquery(document).ready(function () { jquery("#list").jqgrid({ url: '/home/linqgriddata/', datatype: 'json', mtype: 'get', colnames: ['equipmentid', 'categorytitle', 'title'], colmodel: [ { name: 'equipmentid', index: 'equipmentid', width: 40, align: 'left' }, { name: 'categorytitle', index: 'categorytitle', width: 40, align: 'left' }, { name: 'title', index: 'title', width: 200, align: 'left'}], pager: jquery('#pager'), width: 660, height: 'auto', rownum: 10, rowlist: [5, 10, 20, 50], sortname: 'id', sortorder: "desc", viewrecords: true, imgpath: '/scripts/themes/coffee/images', caption: 'my first grid' }); });
<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>
then in controller, have following method suppose return json data
public actionresult linqgriddata(string sidx, string sord, int page, int rows) { assetentities context = new assetentities(); var query = e in context.equipments select e; var count = query.count(); var result = new { total = 1, page = page, records = count, rows = (from e in query select new { id = e.equipmentid, cell = new string[] { e.equipmentid.tostring(), e.category.categorytitle, e.department.title } }).toarray() }; return json(result, jsonrequestbehavior.allowget); }
when run this, code falls on following error
linq entities not recognize method 'system.string tostring()' method
does know how fix error? , also, doing correct way, or should doing different way phil haack explanation since using linq sql?
any feedback appreciated.
thanks folks.
ef doesn't support tostring method, must retrieve data without tostring , format
this should work
public actionresult linqgriddata(string sidx, string sord, int page, int rows) { assetentities context = new assetentities(); var query = e in context.equipments select e; var count = query.count(); var result = new { total = 1, page = page, records = count, rows = query.select(x => new { x.equipamentid, x.category.categorytitle,x.department.title }) .tolist() // .asenumerable() whatever .select(x => new { id = x.equipamentid, cell = new string[] { x.equipamentid.tostring(), x.categorytitle, x.title }}) .toarray(), }; return json(result, jsonrequestbehavior.allowget); }
Comments
Post a Comment