一、当查询得到的数据符合前台要求,不需要做任何处理,直接DataList To Json 返回前台。
代码:
View Code
1 var allEntities = service.QueryByPage(this.GetSearchFilter(searchModel), "InsertedDateUtc Descending", pageSize, searchModel.PageIndex + 1, out recordCount); 2 return Json(allEntities, JsonRequestBehavior.AllowGet);
前台得到的Json数据(两条记录)
1 [ 2 { 3 "DocumentID": "61d09198-198e-403e-89a0-01b98402c8ca", 4 "DocumentFileName": "189017.docx.pdf", 5 "ContentType": "doc", 6 "Size": 167228, 7 "InsertedDateUtc": "/Date(1358762613167)/", 8 "LastModifiedOnDataSource": "/Date(1358504490000)/", 9 "UniqueIDOnDataSource": "189017", 10 "IsActive": true, 11 "HasBeenIndexed": true, 12 "TradeProductID": "1e04dc4d-5a68-4982-ba88-170a3f6525f5", 13 "HasProcessedForAlerts": false, 14 "Congress": "ESMO-2012", 15 "Authors": "Zi Ming Zhao", 16 "CongressType": "Poster", 17 "DocumentTitle": "立普妥-一级预防中的应用 ", 18 "EntityState": 2, 19 "EntityKey": { 20 "EntitySetName": "Document", 21 "EntityContainerName": "MVCExampleEntities", 22 "EntityKeyValues": [ 23 { 24 "Key": "DocumentID", 25 "Value": "61d09198-198e-403e-89a0-01b98402c8ca" 26 } 27 ], 28 "IsTemporary": false 29 } 30 }, 31 { 32 "DocumentID": "a71ea30c-b544-41fa-b008-77adcf7a0250", 33 "DocumentFileName": "189153.docx.pdf", 34 "ContentType": "doc", 35 "Size": 136195, 36 "InsertedDateUtc": "/Date(1358762610573)/", 37 "LastModifiedOnDataSource": "/Date(1358778247000)/", 38 "UniqueIDOnDataSource": "189153", 39 "IsActive": true, 40 "HasBeenIndexed": true, 41 "TradeProductID": "1e04dc4d-5a68-4982-ba88-170a3f6525f5", 42 "HasProcessedForAlerts": false, 43 "Congress": null, 44 "Authors": null, 45 "CongressType": null, 46 "DocumentTitle": "立普妥-碾碎服用 ", 47 "EntityState": 2, 48 "EntityKey": { 49 "EntitySetName": "Document", 50 "EntityContainerName": "MVCExampleEntities", 51 "EntityKeyValues": [ 52 { 53 "Key": "DocumentID", 54 "Value": "a71ea30c-b544-41fa-b008-77adcf7a0250" 55 } 56 ], 57 "IsTemporary": false 58 } 59 } 60 ]
二、当得到的数据不能满足前台需求,比如,我希望加入一个键值对存入总记录数,以方便做分页。
两种方法:
1、拼字符串,注意return 的是Content( ).
代码
1 var allEntities = service.QueryByPage(this.GetSearchFilter(searchModel), "InsertedDateUtc Descending", pageSize, searchModel.PageIndex + 1, out recordCount); 2 3 string json = JsonConvert.SerializeObject(allEntities); 4 StringBuilder sb = new StringBuilder(); 5 sb.Append("{"); 6 sb.Append("\"total\""); 7 sb.Append(":280,"); 8 sb.Append("\"rows\""); 9 sb.Append(":"); 10 sb.Append(json); 11 sb.Append("}"); 12 return Content(sb.ToString()); 13 14 或者直接用String: string jsonString = "{'total':280,'rows':" + json + "}"; (此方法用easyui测试时,前台无法解析)。 15 return Content(jsonString);
前台得到的Json数据
1 { 2 "total": 280, 3 "rows": [ 4 { 5 "$id": "1", 6 "DocumentID": "61d09198-198e-403e-89a0-01b98402c8ca", 7 "DocumentFileName": "189017.docx.pdf", 8 "ContentType": "doc", 9 "Size": 167228, 10 "InsertedDateUtc": "2013-01-21T18:03:33.167", 11 "LastModifiedOnDataSource": "2013-01-18T18:21:30", 12 "UniqueIDOnDataSource": "189017", 13 "IsActive": true, 14 "HasBeenIndexed": true, 15 "TradeProductID": "1e04dc4d-5a68-4982-ba88-170a3f6525f5", 16 "HasProcessedForAlerts": false, 17 "Congress": "ESMO-2012", 18 "Authors": "Zi Ming Zhao", 19 "CongressType": "Poster", 20 "DocumentTitle": "立普妥-一级预防中的应用 ", 21 "EntityKey": { 22 "$id": "2", 23 "EntitySetName": "Document", 24 "EntityContainerName": "MVCExampleEntities", 25 "EntityKeyValues": [ 26 { 27 "Key": "DocumentID", 28 "Type": "System.Guid", 29 "Value": "61d09198-198e-403e-89a0-01b98402c8ca" 30 } 31 ] 32 } 33 }, 34 { 35 "$id": "3", 36 "DocumentID": "a71ea30c-b544-41fa-b008-77adcf7a0250", 37 "DocumentFileName": "189153.docx.pdf", 38 "ContentType": "doc", 39 "Size": 136195, 40 "InsertedDateUtc": "2013-01-21T18:03:30.573", 41 "LastModifiedOnDataSource": "2013-01-21T22:24:07", 42 "UniqueIDOnDataSource": "189153", 43 "IsActive": true, 44 "HasBeenIndexed": true, 45 "TradeProductID": "1e04dc4d-5a68-4982-ba88-170a3f6525f5", 46 "HasProcessedForAlerts": false, 47 "Congress": null, 48 "Authors": null, 49 "CongressType": null, 50 "DocumentTitle": "立普妥-碾碎服用 ", 51 "EntityKey": { 52 "$id": "4", 53 "EntitySetName": "Document", 54 "EntityContainerName": "MVCExampleEntities", 55 "EntityKeyValues": [ 56 { 57 "Key": "DocumentID", 58 "Type": "System.Guid", 59 "Value": "a71ea30c-b544-41fa-b008-77adcf7a0250" 60 }