• 将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>
    View Code

      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
                    ],
                });
            }
        }
    View Code

      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; }
        }
    View Code

      注意:被转换的对象的字段名必须与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

  • 相关阅读:
    Chrome浏览器扩展开发系列之三:Google Chrome浏览器扩展的架构
    Chrome浏览器扩展开发系列之一:初识Google Chrome扩展
    Chrome浏览器扩展开发系列之五:Page Action类型的Chrome浏览器扩展
    Chrome浏览器扩展开发系列之四:Browser Action类型的Chrome浏览器扩展
    鼠标定位问题总结
    Chrome浏览器扩展开发系列之八:Chrome扩展的数据存储
    Chrome浏览器扩展开发系列之七:override页面
    Chrome浏览器扩展开发系列之六:options 页面
    Chrome浏览器扩展开发系列之二:Google Chrome浏览器扩展的调试
    Chrome浏览器扩展开发系列之九:Chrome浏览器的chrome.alarms.* API
  • 原文地址:https://www.cnblogs.com/ceci/p/8489109.html
Copyright © 2020-2023  润新知