• 将前台JS数组序列化后提交,后台反序列化对象


    1. //前台js
      function inputAll(thiss) {
      var _entryArr = [];
      var $trArr = $("#FirstTable tr:gt(0)");
      if ($trArr.length == 0) { return; }
      for (var i = 0; i < $trArr.length; i++) {
      var $thirdTd = $($trArr[i]).find("td:eq(2)");
      var entryObjectID = $thirdTd.find(":hidden:eq(0)").val();
      var entryData = $thirdTd.find(":text:eq(0)").val();
      if ($.trim(entryData).length == 0) { alert('请输入第' + (i + 1) + '行要录入的数据!'); return; }
      var _entry = {};
      _entry.EntryObjectID = entryObjectID;
      _entry.EntryData = entryData;
      _entryArr.push(_entry);
      }
      var _data = {};
      _data.Action = 'InputAllData';
      _data.EntryData_Arr = Serialize(_entryArr);
      _data.EntryTime = $("#txtFirstStartTime").val();
      $.ajax({
      type: "get",
      cache: false,
      url: "OtherData.aspx",
      data: _data,
      beforeSend: function (XMLHttpRequest) {
      showLoading('正在录入,请稍候...');
      },
      success: function (data) {
      try {
      hideLoading();
      if (Number(data) >= 1) {
      alert("批量录入成功!");
      } else {
      alert("批量录入失败!");
      }
      } catch (ex) {
      alert("批量录入失败!");
      }
      },
      error: function () {
      hideLoading();
      }
      });
      }

    2. //后台:
    3. string EntryData = Request["EntryData_Arr"]; 
      //[{EntryObjectID:"7",EntryData:"121.222"},{EntryObjectID:"8",EntryData:"115.3"},{EntryObjectID:"9",EntryData:"166.8"},{EntryObjectID:"1",EntryData:"0"}]
      tempEntryModel[] EntryArr = (tempEntryModel[])Newtonsoft.Json.JsonConvert.DeserializeObject(EntryData, typeof(tempEntryModel[]));

      1. //临时model类

      2. private class tempEntryModel
        {
              //{EntryObjectID:"7",EntryData:"121.222"}
             public int EntryObjectID { get; set; }
             public double EntryData { get; set; }
        }

    4. //js序列化方法:
    5. function Serialize(obj) {
           switch (obj.constructor) {
              case Object:
              var str = "{";
              for (var o in obj) {
                  str += o + ":" + Serialize(obj[o]) + ",";
              }
              if (str.substr(str.length - 1) == ",")
                  str = str.substr(0, str.length - 1);
              return str + "}";
              break;
              case Array:
              var str = "[";
              for (var o in obj) {
                  str += Serialize(obj[o]) + ",";
              }
              if (str.substr(str.length - 1) == ",")
                  str = str.substr(0, str.length - 1);
              return str + "]";
              break;
              case Boolean:
              return """ + obj.toString() + """;
              break;
              case Date:
              return """ + obj.toString() + """;
              break;
              case Function:
              break;
              case Number:
              return """ + obj.toString() + """;
              break;
              case String:
              return """ + obj.toString() + """;
              break;
          }
      }

  • 相关阅读:
    转--- 一些概念不错的理解
    python 生产者 --- 消费者
    python GUI 之 tkinter
    读DataSnap源代码(二)
    读DataSnap源代码(一)
    FireDAC探索 (二)
    FireDAC内部初探
    C++Builder XE7 中“匿名”方法实现
    DelphiXE7 Datasnap TDSClientCallbackChannelManager内部实现初探
    C++ Builder使用VC DLL
  • 原文地址:https://www.cnblogs.com/qiuyueguangxuan/p/4268090.html
Copyright © 2020-2023  润新知