C# MVC 批量修改商品数据

Html:        //批量修改折扣

        $("#Confirm").click(function () {

        //    $.ajaxSettings.async = false;//取消异步

            var Category= $("#Category1").val();//获取商品类别

            var Brand=  $("#Brand1").val();//获取品牌

            var Season= $("#Season1").val();//获取季节

            var plggxszk = $("#plggxszk").val();//获取销售折扣

 

 

            //      $.getJSON("/JH/Commodity/MrketDiscount?Category=" + Category + "&Brand=" + Brand + "&Season=" + Season + "&Discount=" + plggxszk)

            $.ajax({

                url: "/JH/Commodity/MrketDiscount?Category=" + Category + "&Brand=" + Brand + "&Season=" + Season + "&Discount=" + plggxszk,

                type: "get",

                success: function (data) {

                    if (data.State) {

                        layer.msg(data.Text, { icon: 1, skin: "layui-layer-molv" });

 

 

                    }

                }

            })

        })

UIL:

  public ActionResult MrketDiscount(int Category, int Brand, int Season, decimal Discount)

        {

            //实例化返回的字符

            ReturnJson returnJson = new ReturnJson();

 

            var list = (from tbShangPin in myModels.JC_ShangPin

                        select new Discount

                        {

                            ShangPinTypeID = (int)(tbShangPin.ShangPinTypeID),

                            PinPaiID = (int)(tbShangPin.PinPaiID),

                            JiJieID = (int)(tbShangPin.JiJieID),

                            XiaoShozheko = (int)(tbShangPin.XiaoShozheko),

                            ShangPinID = tbShangPin.ShangPinID

                        }).ToList();

            //判断是否选中商品类型

            if (Category > 0)

            {

                list = list.Where(m => m.ShangPinTypeID == Category).ToList();

            }

            if (Brand > 0)

            {

                list = list.Where(m => m.PinPaiID == Brand).ToList();

            }

            if (Season > 0)

            {

                list = list.Where(m => m.JiJieID == Season).ToList();

            }

            if (list.Count > 0)

            {

                for (var i = 0; i < list.Count; i++)

                {

 

                    //声明变量 用于查询修改的商品ID

                    var a = list[i].ShangPinID;

                    JC_ShangPin ShangPin = (from tbSP in myModels.JC_ShangPin

                                            where tbSP.ShangPinID == a

                                            select tbSP).Single();

                    ShangPin.XiaoShozheko = Discount;

                    //list[i].XiaoShozheko = Discount;

                    myModels.Entry(ShangPin).State = System.Data.Entity.EntityState.Modified;

                    myModels.SaveChanges();

                }

            }

            returnJson.State = true;

            returnJson.Text = "批量更新折扣成功!";

            return Json(returnJson, JsonRequestBehavior.AllowGet);

        }

效果图:

C# MVC 批量修改商品数据_第1张图片

说明:先查询出需要修改的数据有多少条,再根据查询出了的商品ID,用for循环循环修改数据就可以了。

你可能感兴趣的:(C# MVC 批量修改商品数据)