MVC(EF) 序列化JSON对象集合,后台接收

4141人阅读 2021/3/22 11:32 总访问:670325 评论:0 收藏:0 手机
分类: Jquery

定义空的JSON对象集合

 var JosnObj = [];

添加JSON对象

          $(".isok").each(function () {
                var Josnstr = {};
                var inputs = $(this).parents("tr").find("input")
                
                Josnstr.sid = inputs.val();
                Josnstr.username = inputs.eq(1).val()
                Josnstr.Number = inputs.eq(2).val()
                Josnstr.Class = inputs.eq(3).val()
                Josnstr.state = $(this).parents("tr").find("select").val()
               
                JosnObj.push(Josnstr)
               
            })

JSON对象数组有了数据



通过JSON.stringify()转化成为JSON字符串

//用AJAX把JSON字符串提交到后台
                     $.ajax({
                url: "/Home/SelectUpdate",
                type: "POST",
                data: { strobj: JSON.stringify(JosnObj) },
                success: function (result) {
                    if (result>0) {
                        location.reload();
                    }
                },
                error: function () {
                    console.log("提交ajax函数异常");
                },
            })

后台部分

           //引入转换JSON字符串的命名空间
           using Newtonsoft.Json;


           //定义字符串接收
           public ActionResult SelectUpdate(string strobj)
            {
            oaEntities oaentities = new oaEntities();
            //通过JsonConvert  将JSON对象集合字符串转换为后台对象集合
            List<UserInfo> list = JsonConvert.DeserializeObject<List<UserInfo>>(strobj);
            //遍历更新数据
                foreach (UserInfo item in list)
                {
                 oaentities.Entry(item).State = System.Data.EntityState.Modified;
                }
               //写入数据库
               int count = oaentities.SaveChanges();
           
               return Json(count);
            }


评价
脚踏实地,一步一个脚印
排名
6
文章
6
粉丝
16
评论
8
{{item.articleTitle}}
{{item.blogName}} : {{item.content}}
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2024TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
欢迎加群交流技术