通过获取客户端Json数据字符串,反序列化为实体对象的一段代码

#region 保存候选人数据
        /// <summary>
        /// 保存候选人数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [AdminAuthorize]
        [ValidateInput(false)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Save(matchjob.Domain.Candidate entity)
        {
            if (entity.Id == 0)
            {
                entity.CreateTime = DateTime.Now;
                this.CandidateManager.Save(entity);

                var json = Request.Form["candidateItemsJsonValue"];
                if (String.IsNullOrEmpty(json))
                {
                    goto LABEL_END;
                }

                using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json)))
                {
                    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(matchjob.Web.UI.Models.TTTT_A));
                    var a = (matchjob.Web.UI.Models.TTTT_A)serializer.ReadObject(ms);

                    foreach (var p in a.Positions)
                    {
                        var item = new matchjob.Domain.CandidateItem();
                        item.Position = p.Name;
                        item.Candidate = entity;
                        item.CreateTime = DateTime.Now;
                        item.UpdateTime = item.CreateTime;
                        CandidateItemManager.Save(item);

                        foreach (var u in p.Users)
                        {
                            int Score, Experience, Interest, Salary;
                            Int32.TryParse(u.Score, out Score);
                            Int32.TryParse(u.Experience, out Experience);
                            Int32.TryParse(u.Interest, out Interest);
                            Int32.TryParse(u.Salary, out Salary);

                            var item2 = new matchjob.Domain.CandidateUser();
                            item2.CandidateItem = item;
                            item2.Remark = u.Remark;
                            item2.Score = Score;
                            item2.UserInfo = new UserInfo();
                            item2.UserInfo.Id = u.UserInfoId;
                            item2.Experience = Experience;
                            item2.Interest = Interest;
                            item2.Salary = Salary;
                            item2.CreateTime = DateTime.Now;
                            item2.UpdateTime = item.CreateTime;
                            CandidateUserManager.Save(item2);
                        }
                    }
                }

                LABEL_END:
                {

                }
            }
            else
            {
                var model = this.CandidateManager.Get(entity.Id);
                model.CompanyName = entity.CompanyName;
                model.ValidateCode = entity.ValidateCode;

                this.CandidateManager.Update(model);

                var json = Request.Form["candidateItemsJsonValue"];
                if (String.IsNullOrEmpty(json))
                {
                    goto LABEL_DEL;
                }

                using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json)))
                {

                    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(matchjob.Web.UI.Models.TTTT_A));
                    var a = serializer.ReadObject(ms) as matchjob.Web.UI.Models.TTTT_A;

                    foreach (var p in a.Positions)
                    {
                        var item = new matchjob.Domain.CandidateItem();
                        var delUsers = new List<matchjob.Domain.CandidateUser>();
                        item.Id = p.Id;
                        item.Position = p.Name;
                        item.Candidate = model;

                        if (p.Id != 0)
                        {
                            item = CandidateItemManager.Get(p.Id);
                            item.Position = p.Name;
                        }

                        if (p.Id != 0)
                        {
                            item.UpdateTime = DateTime.Now;
                            CandidateItemManager.Update(item);

                            delUsers = item.CandidateUsers.Where(CUser => !p.Users.Contains(new Models.TTTT_C { UserInfoId = CUser.UserInfo.Id }, new CandidateUserComparer())).ToList();
                        }
                        else
                        {
                            item.CreateTime = DateTime.Now;
                            item.UpdateTime = item.CreateTime;
                            CandidateItemManager.Save(item);
                        }

                        ///更新和增加User
                        foreach (var u in p.Users)
                        {
                            int Score, Experience, Interest, Salary;
                            Int32.TryParse(u.Score, out Score);
                            Int32.TryParse(u.Experience, out Experience);
                            Int32.TryParse(u.Interest, out Interest);
                            Int32.TryParse(u.Salary, out Salary);

                            var item2 = new matchjob.Domain.CandidateUser();
                            item2.CandidateItem = item;
                            item2.Remark = u.Remark;
                            item2.Score = Score;
                            item2.UserInfo = new UserInfo();
                            item2.UserInfo.Id = u.UserInfoId;
                            item2.Id = u.Id;
                            item2.Experience = Experience;
                            item2.Interest = Interest;
                            item2.Salary = Salary;

                            if (u.Id != 0)
                            {
                                item2 = CandidateUserManager.Get(u.Id);

                                item2.CandidateItem = item;
                                item2.Remark = u.Remark;
                                item2.Score = Score;
                                item2.Experience = Experience;
                                item2.Interest = Interest;
                                item2.Salary = Salary;
                            }

                            if (u.Id != 0)
                            {
                                item2.UpdateTime = DateTime.Now;
                                CandidateUserManager.Update(item2);
                            }
                            else
                            {
                                item2.CreateTime = DateTime.Now;
                                item2.UpdateTime = item2.CreateTime;
                                CandidateUserManager.Save(item2);
                            }
                        }
                        ///删除User
                        foreach (var u in delUsers)
                        {
                            foreach(var f in CandidateForwardUserManager.GetByCandidateUserId(u.Id))
                            {
                                CandidateForwardUserManager.Delete(f.Id);
                            }
                            CandidateUserManager.Delete(u.Id);
                        }
                    }
                }

                LABEL_DEL:
                ///删除已经移出的职位数据
                foreach (var itemid in Request.Form["candidateItemsDel"].Split(new char[] { , }, StringSplitOptions.RemoveEmptyEntries))
                {
                    var users = CandidateUserManager.GetsByCandidateItemId(Convert.ToInt64(itemid));

                    for (int k = 0; k < users.Count; k++)
                    {
                        ///先删除关联对象
                        foreach (var f in CandidateForwardUserManager.GetByCandidateUserId(users[k].Id))
                        {
                            CandidateForwardUserManager.Delete(f.Id);
                        }
                        CandidateUserManager.Delete(users[k].Id);
                    }

                    ///先删除关联对象
                    foreach (var f in CandidateForwardManager.GetByCandidateItemId(Convert.ToInt64(itemid)))
                    {
                        CandidateForwardManager.Delete(f.Id);
                    }
                    CandidateItemManager.Delete(Convert.ToInt64(itemid));
                }
            }

            return Json(new { IsSuccess = true, Message = "保存成功" }, "text/plain", JsonRequestBehavior.AllowGet);
        }
        #endregion

    public class TTTT_e
    {
        public TTTT_B[] Positions { get; set; }
    }

    public class TTTT_B
    {
        public long Id { get; set; }
        public String Name { get; set; }
        public TTTT_C[] Users { get; set; }
    }

    public class TTTT_C
    {
        public long Id { get; set; }
        public long UserInfoId { get; set; }
        public String Score { get; set; }
        public String Remark { get; set; }
        public String Name { get; set; }

        public String Experience { get; set; }
        public String Interest { get; set; }
        public String Salary { get; set; }
    }

 

 

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。