首页 > 代码库 > 表单数据批量提交,后台批量接收并保存

表单数据批量提交,后台批量接收并保存

前台JS代码

 1  $("#btn_save").click(function (data) {
 2             var values = $("#from_jbxx").serializeArray();
 3             var strdata =http://www.mamicode.com/ {};
 4             $.each($("#from_jbxx").serializeArray(), function (a, b) { strdata[b.name] = b.value; });
 5             if ($("#from_jbxx").validationEngine(‘validate‘)) {
 6 
 7                 $.ajax({
 8                     type: ‘POST‘,
 9                     url: getFnDataUrl("HttpAshx/StudentInfo.ashx", "SaveStudentTurnover"),
10                     data: JSON.stringify(strdata),
11                     success: function (strResult) {
12                         var res = $.parseJSON(strResult) || {};
13                         if (res.Succeed) {
14                             alert(res.Message);
15                             location.href = "http://www.mamicode.com/Student/UserCenter.aspx";
16                         } else {
17                             alert(res.Message);
18                         }
19                     }
20                 });
21                 return false;
22             } else {
23                 return false;
24             }
25         });

后台代码

        public void SaveStudentTurnover(HttpContext context)
        {

            byte[] bytes = new byte[context.Request.InputStream.Length];
            context.Request.InputStream.Read(bytes, 0, bytes.Length);
            var values = System.Text.Encoding.UTF8.GetString(bytes).JsonDeserialize<Dictionary<string, string>>();

            string strId = values["Id"] ?? "";
            var model = _gdc.GetDataSet<XjStudentTurnover>().FirstOrDefault(p => p.Id == strId);
            if (model == null)
            {
                model = XjStudentTurnover.CreateXjStudentTurnover(_gdc.GetNewCode<XjStudentTurnover>());
                model.InnerDeleteFlag = false;
                model.StudentId = values["StudentId"] ?? "";
                _gdc.AddObject(model);
            }

            model.LoadFrom(values);
            _gdc.SaveChanges();
            DtHttpContext.ReturnMesg(AjaxResult.Success("保存成功~!"), context);
            return;
        }

  

表单数据批量提交,后台批量接收并保存