修改

开发工具与关键技术:修改

作者:周乐献

撰写时间:2019/5/8

数据的修改

如果数据出现了错误或者想要更新数据就要修改它,那么怎么修改数据呢?

首先运用单击事件打开修改模态框

function openUpdate(xueyuanId)

        {

            重置表单

            $('#formUpdatexueyuan input[type="reset"]').click();

            回填数据

            $.post("/Main/xueyuan", { xueyuanId: UxueyuanId },

                function (data) {

                    loadDatatoForm("formUpdateXueyuan", data);

根据json对象填充form表单

                },"json")

            弹出模态框

            $("#modalUpdateXueyuan").modal('show');

        }

 

        保存修改

        function savaUpdate()

        {

            获取页面数据

            var XueyuanID = $("#UXueyuanID").val()

            var XueyuanName = $("#UXueyuanName").val()

            var XueyuanCode = $("#UXueyuanCode").val()

 

            判断

            if (XueyuanID != '' && XueyuanID != undefined && !isNaN(XueyuanID)

                && XueyuanName != '' && XueyuanName != undefined

                && XueyuanCode != '' && XueyuanCode != undefined)

            {

                提交数据

                $.post("/Main//UpdateXueyuan",

                    {

                        XueyuanID: XueyuanID,

                        XueyuanName: XueyuanName,

                        XueyuanCode: XueyuanCode

                    },

                    function (returnJson) {

                        if (returnJson.State == true) {

                            关闭模态框

                            $("#modalUpdateXueyuan").modal('hide');

                        

                            tabXueyuan = layuiTable.reload('tabXueyuan');

                        }

                        layer.alert(returnJson.Text);

                    }, "json");

            } else

            {

                提示

                layer.alert('请填写完整', { title: '提示', icon: 0 });

            }

        }

修改_第1张图片

和数据库数据进行绑定修改

public ActionResult UpdateXueyuan(PW_xueyuan pwXueyuan)

        {

            ReturnJson returnJson = new ReturnJson();

            try

            {

                int otherCount = (from tbXueyuan in myModel.PW_xueyuan

                                  where tbXueyuan.XueyuanID!=pwXueyuan.XueyuanID&&(

                                  tbXueyuan.XueyuanName==pwXueyuan.XueyuanName.Trim()||

                                  tbXueyuan.XueyuanCode == pwXueyuan.XueyuanCode.Trim())

                                  select tbXueyuan).Count();

                if (otherCount == 0)

                {

                    选择要修改的数据

                    PW_xueyuan dbXueyuan = (from tbXueyuan in myModel.PW_xueyuan

                                                根据学院ID提取数据

                                             where tbXueyuan.XueyuanID == pwXueyuan.XueyuanID

                                             select tbXueyuan).Single();

通过学院表进行实体赋值

                    dbXueyuan.XueyuanName = pwXueyuan.XueyuanName;

                    dbXueyuan.XueyuanCode = pwXueyuan.XueyuanCode;

                    执行修改

                    myModel.Entry(dbXueyuan).State = EntityState.Modified;

                    保存修改

                    if (myModel.SaveChanges() > 0)

                    {

                        returnJson.State = true;

                        returnJson.Text = "修改成功";

                    } else {

                        returnJson.State = false;

                        returnJson.Text = "修改失败";

                    }

                }else {

                    returnJson.State = false;

                    returnJson.Text = "和其他学院重复";

                }

判断与其他学院名称进行重复没有,如果重复则不能修改

            }

            catch (Exception e)

            {

                Console.WriteLine(e);

                returnJson.State = false;

                returnJson.Text = "数据异常";

            }

 

            return Json(returnJson, JsonRequestBehavior.AllowGet);

}

 

修改_第2张图片

     修改_第3张图片

 

你可能感兴趣的:(修改)