Example #1
0
        public ActionResult Edit(string id, ContactInfo model)
        {
            //合法性验证
            if (id.IsInt() == false || id == null)
            {
                return(Content("<script>alert('数据不存在或编辑错误');window.location='/CInfo/Index'</script>"));
            }

            phonebookEntities db = new phonebookEntities();
            int iid = id.AsInt();

            //TODO:第一种编辑方式
            //var entity = db.ContactInfo.FirstOrDefault(c => c.ID == iid);

            //entity.ContactName = model.ContactName;
            //entity.CommonMobile = model.CommonMobile;
            //entity.GroupId = model.GroupId;

            //db.SaveChanges();

            //TODO: 第二种编辑方式
            model.ID = id.AsInt();
            DbEntityEntry entry = db.Entry(model);

            entry.State = System.Data.EntityState.Unchanged;
            entry.Property("ContactName").IsModified  = true;
            entry.Property("CommonMobile").IsModified = true;
            entry.Property("GroupId").IsModified      = true;

            db.Configuration.ValidateOnSaveEnabled = false;     //关闭检查

            db.SaveChanges();

            return(Content("<script>alert('编辑成功');window.location='/CInfo/Index'</script>"));
        }
Example #2
0
        public ActionResult Edit(int id, ContactInfoView model)
        {
            //0.0 检查model实体的属性的合法性
            if (ModelState.IsValid == false)
            {
                //验证失败,则应该跳转回edit.cshtml视图,同时增加一个错误提醒
                SetGlist();
                //向视图增加一个错误提醒,配合视图中的@Html.ValidateionSummary(true) 来使用
                ModelState.AddModelError("", "实体验证失败,请检查");

                return(View());
            }

            try
            {
                //1.0 通过EF的推荐方式 ,先查在修改
                phonebookEntities db = new phonebookEntities();
                //var entity = db.ContactInfo.FirstOrDefault(c => c.ID == id);
                //entity.CommonMobile = model.CommonMobile;
                //entity.ContactName = model.ContactName;
                //entity.GroupId = model.GroupId;
                //db.SaveChanges();

                //2.0 通过自定义实体的方式 ,先追加再修改状态
                //补全id属性的值
                model.ID = id;  //update ContactInfo set CommonMobile=,ContactName=,GroupId where iD=1
                //将model追加到EF容器中
                ContactInfo entity = new ContactInfo()
                {
                    ID           = model.ID,
                    GroupId      = model.GroupId,
                    ContactName  = model.ContactName,
                    CommonMobile = model.CommonMobile
                };
                //将entity追加到EF容器中
                System.Data.Entity.Infrastructure.DbEntityEntry enty = db.Entry(entity);
                //修改其状态
                enty.State = System.Data.EntityState.Unchanged;
                //将要修改的属性的IsModified设置成true
                enty.Property("CommonMobile").IsModified = true;
                enty.Property("ContactName").IsModified  = true;
                enty.Property("GroupId").IsModified      = true;
                //关闭EF的实体检查
                db.Configuration.ValidateOnSaveEnabled = false;

                //统一将sql语句发送给db执行
                db.SaveChanges();

                //直接跳转到 cinfo/index 页面
                return(RedirectToAction("Index"));
            }
            catch (Exception ex)
            {
                SetGlist();
                ModelState.AddModelError("", ex.Message);
                return(View());
            }
        }