webapi返回泛型给easyui
2016-04-14 14:15
316 查看
由于之前遇到的easyui调用webapi的问题。
参见 :/article/9495115.html
所以就考虑,封装一个泛型用来返回 HttpResponseMessage
直接上代码:
泛型封装:
webapi:
这里是返回DataTable类型
这里是返回对象类型
前台HTML :
参见 :/article/9495115.html
所以就考虑,封装一个泛型用来返回 HttpResponseMessage
直接上代码:
泛型封装:
public class APIResult<T> : HttpResponseMessage { private T Data; public APIResult(T Data, string DataType = "application/json") { this.Data = Data; string jsonStr = GetJson(this.Data); this.Content = new StringContent(jsonStr, System.Text.Encoding.UTF8, DataType); } //返回值 public bool ResponseResult = false; //返回message public string ResponseMsg = "failed"; private string GetJson(T Data) { if (Data is string || Data is StringBuilder) return Data.ToString(); else if (Data is DataTable) return JsonConvert.SerializeObject(Data, new DataTableConverter()); else return JsonConvert.SerializeObject(Data); } }
webapi:
这里是返回DataTable类型
public APIResult<DataTable> GetProductParentType(string PT_Name=null, string PT_Code=null, int PT_ParentID = 0) { DataTable dt = pt.GetProductParentType(PT_Name, PT_Code,PT_ParentID); APIResult<DataTable> result = new APIResult<DataTable>(dt); return result; }
这里是返回对象类型
public APIResult<Rootobject> Get(int id) { Rootobject resp=new Rootobject(); APIResult<Rootobject> api = new APIResult<Rootobject>(resp); return api; }
前台HTML :
<div id="gdv_ProductParentType"></div> <script> $('#gdv_ProductParentType').datagrid({ url: '../api/BindData/GetProductParentType', method: 'get', //默认是post,不允许对静态文件访问 width: '700', rownumbers: true, columns: [[ { field: 'PT_Name', title: 'PT_Name' }, { field: 'PT_CreateTime', title: 'PT_CreateTime', formatter: function (value, row, index) { var dt = new Date(value); return dt.toLocaleDateString();//设置时间格式 } } ]], onClickRow: function (index, row) { }, singleSelect: true, selectOnCheck: true }); </script>
相关文章推荐
- IOS-UI学习笔记(超级猜图)
- Android中关于Volley的使用(十)对Request和Reponse的认识
- Android中关于Volley的使用(五)从RequestQueue开始来深入认识Volley
- Pywinauto Guideline
- iOS 对UITextView以及UITextField的字数限制.
- UISearchController使用中的几个坑和一个知识点
- 2016.04.14,英语,《Vocabulary Builder》Unit 14
- OpenCV学习笔记(二十一)——绘图函数core OpenCV学习笔记(二十二)——粒子滤波跟踪方法 OpenCV学习笔记(二十三)——OpenCV的GUI之凤凰涅槃Qt OpenCV学习笔记(二十
- Packet for query is too large(mysql写入数据过大)
- Light OJ 1277 Looking for a Subsequence
- 修改UISegmentrol各种属性
- Android之线程回掉更新ui
- winbuilder安装及使用
- android studio多渠道打包,且根据buildConfig属性判断为哪个渠道升级或者实现不同业务逻辑
- IOS UIImage类方法总结
- Android中的UI细节
- 性能更好的js动画实现方式---requestAnimationFrame
- Windows Azure 系列-- Azure Queue的操作
- 利用UITableView实现个人信息界面
- 期望 zoj3929 Deque and Balls