在使用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 == 0) { 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