在使用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的更多相关文章

  1. JavaScript中创建类,赋值给ajax中的data参数

    缘由:因为要给根据是否选中checkbox来动态增加ajax中data的属性(ajax的data属性格式的几种方法,参考http://www.jb51.net/article/46676.htm) d ...

  2. ajax传参data里面的键是一个变量的解决方法

    直接用这种方式来传参,比如bean中有字段 username password,则是 data[username] = "用户名"; data[password] = " ...

  3. ajax请求的data数据格式

    ajax提交data类型 一.问题来源 今天使用ajax时,发现get传data时,传递json字符串时传不过去参数,所以做了一些实验测试ajax的get和post的传递data时的不同. 二.概念 ...

  4. Controller怎么接收Ajax传来的data

    var json = { "VendorId": strVendorId, "VendorName": strVendorName, "Remark& ...

  5. ajax就收data的参数

    一,变量 "data": ${cityData},//数据(必传) 二,json data:{"state":"Front"},

  6. jquery.form.js实现将form提交转为ajax方式提交的使用方法

    本文实例讲述了jquery.form.js实现将form提交转为ajax方式提交的方法.分享给大家供大家参考.具体分析如下: 这个框架集合form提交.验证.上传的功能. 这个框架必须和jquery完 ...

  7. jquery.form.js实现将form提交转为ajax方式提交的方法

    本文实例讲述了jquery.form.js实现将form提交转为ajax方式提交的方法.分享给大家供大家参考.具体分析如下: 这个框架集合form提交.验证.上传的功能. 这个框架必须和jquery完 ...

  8. 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 ...

  9. 一些ajax代码

    $.ajax({ type : "get", url : "list_hot_ajax.json", data : {"provinceId" ...

随机推荐

  1. Difference between the Bill of distribution and sourcing rule.

        https://forums.oracle.com/thread/936768 This is from a users guide  Oracle Supply Chain Planning ...

  2. Swiper使用方法(向前和向后按钮在swiper-container外面)

    Swiper插件的使用 1.HTML <!-- Swiper --> <section class="swipper"> <div class=&qu ...

  3. LINQ to objects遇到的小坑

    1.C#中LINQ to Objects中延迟查询的陷阱(其他类型的LINQ也基本一致) 之前在不了解LINQ延迟查询的时候,我使用下面的这种方式,将where语句的结果直接as为List<T& ...

  4. 缓存 - 内存数据库Redis

    客户端 Redis Desktop Manager 官网 新版的收费,或者要分享什么的.0.8.8的旧版才免费?:https://github.com/uglide/RedisDesktopManag ...

  5. Java各国首都列表

    国 家 名 称 首  都 中华人民共和国 People's Republic of China 北京 Beijing 蒙古 Mongolia 乌兰巴托 Elggydggmgj 朝鲜 Democrati ...

  6. log Log4NET配置

    Log4Net是用来记录日志的,可以将程序运行过程中的信息输出到一些地方(文件.数据库.EventLog等),日志就是程序的黑匣子,可以通过 日志查看系统的运行过程,从而发现系统的问题.日志的作用:将 ...

  7. JQuery - 动态添加Html后,如何使CSS生效,JS代码可用?

    今天在开发JQuery Mobile程序时候,需要从服务器取得数据,随后显示在页面上的Listview控件中,数据完整获取到了,也动态添加到Listview控件中,但是数据对应的CSS没有任何效果了, ...

  8. JQuery - Ajax和Tomcat跨域请求问题解决方法!

    在JQuery里面使用Ajax和Tomcat服务器之间进行数据交互,遇到了跨域请求问题,无法成功得到想要的数据! 错误信息部分截图: 通过错误信息判断知道已经发生在Ajax跨域请求问题了! 当前Tom ...

  9. JavaScript基础数组_布尔值_逻辑运算等(2)

    day51 参考:https://www.cnblogs.com/liwenzhou/p/8004649.html 布尔值(Boolean) 区别于Python,true和false都是小写. var ...

  10. 萝卜保卫战3内购破解+Toast窗口增加(Love版)

    涉及到一些不同的破解的方法,以及不同的破解思路,还有一些重要权限的删除等. 作者:HAI_ 这次目标是经常玩的萝卜保卫战,不知不觉,已经更新到3了.详细分析请参考https://bbs.ichunqi ...