将AJAX Post的Data转为对应的Class
在使用DataTables从服务端获取数据时,在非MVC的情况下没有MVC的自动绑定功能,所以需要自己写一个绑定,将Post过来的InputStream转为对应的类。
HTML:
<form id="formSearch" runat="server" class="form-horizontal">
<div class="container-fluid">
<div class="form-group">
<asp:Label CssClass="col-sm-2 control-label" runat="server" Text="<%$ Resources:Resource,Form_Id1 %>"></asp:Label>
<div class="col-sm-4">
<input type="text" class="form-control" id="formID" name="formID" maxlength="64" />
</div> <asp:Label CssClass="col-sm-2 control-label" runat="server" Text="<%$ Resources:Resource,Process_Name1 %>"></asp:Label>
<div class="col-sm-4">
<select class="form-control" id="FlowID" name="FlowID">
</select>
</div>
</div>
<div class="form-group">
<asp:Label runat="server" CssClass="col-sm-2 control-label" Text="<%$ Resources:Resource,Apply_Person1 %>"></asp:Label>
<div class="col-sm-4">
<input type="text" id="applyPerson" name="applyPerson" class="form-control" maxlength="64" />
</div>
<div class="col-sm-2">
<button id="btnSearch" class="btn btn-primary">
<asp:Literal runat="server" Text="<%$ Resources:Resource,Search %>"></asp:Literal>
</button>
</div>
</div>
</div>
</form>
JS:
function BindList() {
if (table == null) {
table = $list.DataTable({
ajax:
{
url: "/PageHandle/TaskHandler.ashx?type=MyTask",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: function (d) {
return JSON.stringify($.extend({}, d, $.getJsonFromForm($("#formSearch"))));
}
},
columns: [
{
XXXXXXXXX
],
});
}
}
TaskRQ:
public class TaskRQ : BaseDataTablesRequest
{
//public bool isTempNameZh_cn { get; set; } public string currentUser { get; set; }
public string urgentCase { get; set; }
public string FlowID { get; set; }
public string taskStatus { get; set; }
public string formID { get; set; }
public string activityName { get; set; }
public string applyPerson { get; set; }
public bool isChina { get; set; }
public DateTime? applyDateFrom { get; set; }
public DateTime? applyDateTo { get; set; }
//public DateTime? assignDateFrom
//{
// get; set;
//}
//public DateTime? assignDateTo { get; set; }
//public string orderBy { get; set; }
}
注意:被转换的对象的字段名必须与Html中的Name字段一致,否则将忽略该对象的赋值。
使用JavaScriptSerializer来将InputStream转为TaskRQ对象。
public void ProcessRequest(HttpContext context)
{
var rq = GetTaskRQ(context);
var taskS = new WFTaskS();
object rp ;
var type = context.Request["type"];
switch (type)
{
case "MyTask":
rp = taskS.GetMyTask(rq);
break;
case "MyChildTask":
rp = taskS.GetMyChildTask(rq);
break;
case "MyDelegatedTask":
rp = taskS.GetDelegatedTask(rq);
break;
case "AllTask":
rp = taskS.GetAllTask(rq);
break;
default:
rp = taskS.GetMyTask(rq);
break;
}
var js = new JavaScriptSerializer();
var result = js.Serialize(rp);
context.Response.ContentType = "application/json; charset=utf-8";
context.Response.Write(result);
} public bool IsReusable
{
get
{
return false;
}
} /// <summary>
/// 获得Post来的InputStream,将其转化为Json,并将Json反序列化为对象
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
private TaskRQ GetTaskRQ(HttpContext context)
{
var stream = context.Request.InputStream;
if (stream.Length == )
{
return null;
}
var reader = new StreamReader(stream);
var json = reader.ReadToEnd();
var js = new JavaScriptSerializer();
var rq = js.Deserialize<TaskRQ>(json);
var IsChina = false;
if (HttpContext.Current.Session != null
&& HttpContext.Current.Session["_Localization"] != null
&& HttpContext.Current.Session["_Localization"].ToString().ToLower() == "zh-cn")
{
IsChina = true;
}
rq.isChina = IsChina;
return rq;
}
还有其他的一些方式,可以参见http://blog.csdn.net/testcs_dn/article/details/78150046?locationNum=6&fps=1
将AJAX Post的Data转为对应的Class的更多相关文章
- JavaScript中创建类,赋值给ajax中的data参数
缘由:因为要给根据是否选中checkbox来动态增加ajax中data的属性(ajax的data属性格式的几种方法,参考http://www.jb51.net/article/46676.htm) d ...
- ajax传参data里面的键是一个变量的解决方法
直接用这种方式来传参,比如bean中有字段 username password,则是 data[username] = "用户名"; data[password] = " ...
- ajax请求的data数据格式
ajax提交data类型 一.问题来源 今天使用ajax时,发现get传data时,传递json字符串时传不过去参数,所以做了一些实验测试ajax的get和post的传递data时的不同. 二.概念 ...
- Controller怎么接收Ajax传来的data
var json = { "VendorId": strVendorId, "VendorName": strVendorName, "Remark& ...
- ajax就收data的参数
一,变量 "data": ${cityData},//数据(必传) 二,json data:{"state":"Front"},
- jquery.form.js实现将form提交转为ajax方式提交的使用方法
本文实例讲述了jquery.form.js实现将form提交转为ajax方式提交的方法.分享给大家供大家参考.具体分析如下: 这个框架集合form提交.验证.上传的功能. 这个框架必须和jquery完 ...
- jquery.form.js实现将form提交转为ajax方式提交的方法
本文实例讲述了jquery.form.js实现将form提交转为ajax方式提交的方法.分享给大家供大家参考.具体分析如下: 这个框架集合form提交.验证.上传的功能. 这个框架必须和jquery完 ...
- Post data using ajax in laravel 5
转自:http://www.tuicool.com/articles/2u2mmmu Post data using ajax in laravel 5 to controller If you ar ...
- 一些ajax代码
$.ajax({ type : "get", url : "list_hot_ajax.json", data : {"provinceId" ...
随机推荐
- VS2010+Oracle11+Entity Framework4.1环境搭建及常见问题
在微软的实体数据模型中存在四种查询方式: SQL字符串:Linq:Linq to SQL:Linq to Entity(ESQL) 对于Linq SQL目前微软虽然仍在支持,但微软已经声明不再推荐. ...
- Linux C++ - IP地址转换函数
1. 函数用途:数字网络序本地序转换 适用类型:IP地址uint32_t类型.端口号uint16_t类型 #include<netinet/in.h> extern uint32_t nt ...
- bootstrap-treeview + angular 使用
bootstrap-treeview是什么 bootstrap-treeview是一款效果非常酷的基于bootstrap的jQuery多级列表树插件. 怎样使用bootstrap-treeview 插 ...
- LINQ to objects遇到的小坑
1.C#中LINQ to Objects中延迟查询的陷阱(其他类型的LINQ也基本一致) 之前在不了解LINQ延迟查询的时候,我使用下面的这种方式,将where语句的结果直接as为List<T& ...
- hdu 4325 Flowers(区间离散化)
http://acm.hdu.edu.cn/showproblem.php?pid=4325 Flowers Time Limit: 4000/2000 MS (Java/Others) Mem ...
- Restframework 权限permission 组件实例-2
1.在视图类里添加权限组件 class BookView(APIView): authentication_classes = [UserAuth] permission_classes = [SVI ...
- TCP BBR - 一键安装最新内核并开启 TCP BBR
原文地址: https://teddysun.com/489.html 最近,Google 开源了其 TCP BBR 拥塞控制算法,并提交到了 Linux 内核,从 4.9 开始,Linux 内核已经 ...
- jquery判断滚动到某个div显示底部按钮
判读滚动某个div显示底部按钮 <!DOCTYPE html> <html lang="zh-CN"> <head> <meta char ...
- Android访问网络,使用HttpURLConnection还是HttpClient?
本文转自:http://blog.csdn.net/guolin_blog/article/details/12452307,感谢这位网友的分享,谢谢. 最近在研究Volley框架的源码,发现它在HT ...
- vue中axios访问Java后端跨域问题解决
问题背景: 前后端分离,前端选用Vue,后端选用Java,vue编译出的静态页面采用ngix发布,在前端访问后端时出现跨域问题. 解决方法: 跨域的问题解决方法有好多种,这里是通过服务端解决,以下是代 ...