序列化

//#region 保存按钮,保存为草稿

    $(".saveButton").click(function () {

        var MaintenanceOrPartsNo = $('#MaintenanceOrPartsNo').val();

       $.messager.confirm('确认', '您确认保存吗?', function (r) {

            if (r) {

               $.post("/Maintenances/BaoCun", { mainInfo: decodeURIComponent(JSON.stringify($("#myform").serializeArray())), }, function (data){

                   if (data.issuccess) {

                        //$.messager.confirm("操作提示", "保存成功,是否跳到保养一览!", function (r) {

                        //    if (r) {

                        toTab("", "/Maintenances/MaintenanceLists", "保养一览");

                        var jq = top.jQuery;

                        var tt = jq('#tt');

                        tt.tabs("close", "保养详情1" + MaintenanceOrPartsNo);

                        //    }

                       //});

                   } else {

                        $.messager.alert("操作提示", "保存失败!");

                   }

               });

            }

        });

    });

//#endregion

 

后台controller

#region 保存功能

        public JsonResult BaoCun(string mainInfo)

        {

            int result = 0;

            string msg = "操作失败";

            try

            {

               Maintenance model =new Maintenance();

               List<formModel> objModel = newSystem.Web.Script.Serialization.JavaScriptSerializer().Deserialize<List<formModel>>(mainInfo);

               foreach (formModel info in objModel)

               {

                   if (!string.IsNullOrEmpty(info.value))

                   {

                        if (info.name.Equals("MaintenanceOrPartsNo"))

                           model.MaintenanceOrPartsNo = info.value;

                   }

               }

               foreach (formModel info in objModel)

               {

                   PropertyInfo[]propertys = model.GetType().GetProperties();

                   foreach (PropertyInfo p in propertys)

                   {

                        if (!string.IsNullOrEmpty(info.value))

                        {

                            if (p.Name.Equals(info.name))

                                if (p.PropertyType == typeof(string))

                                {

                                   p.SetValue(model, info.value, null);

                                }

                               else if (p.PropertyType == typeof(int) || p.PropertyType == typeof(Nullable<int>))

                                {

                                   p.SetValue(model, int.Parse(info.value.ToString()),null);

                                }

                                else if (p.PropertyType == typeof(DateTime) || p.PropertyType== typeof(Nullable<DateTime>))

                                {

                                   p.SetValue(model, Convert.ToDateTime(info.value.ToString()),null);

                                }

                                else if (p.PropertyType == typeof(float) || p.PropertyType == typeof(Nullable<float>))

                                {

                                    p.SetValue(model, float.Parse(info.value.ToString()), null);

                                }

                                else if (p.PropertyType == typeof(double) || p.PropertyType == typeof(Nullable<double>))

                                {

                                   p.SetValue(model, double.Parse(info.value.ToString()),null);

                                }

 

                        }

                        else

                            continue;

                   }

               }

               Dictionary<string, dynamic> dic = new Dictionary<string, dynamic>();

               dic.Add("Maintenance", model);

               result = MaintenanceBLL.InsertList(dic);

               if (result > 0)

               {

                   msg = "操作成功";

               }

               else

                   msg = "操作失败";

            }

            catch (Exception ex)

            {

               result = 0;

               msg = "操作失败";

            }

            object obj = new

            {

               issuccess = (result > 0 ? true : false),

               msg = msg

           };

            return Json(obj);

        }

        #endregion

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值