Ejemplo n.º 1
0
        public Form1()
        {
            //create database instance
            database = new EntityFramework.phonebookEntities();

            InitializeComponent();
        }
Ejemplo n.º 2
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>"));
        }
Ejemplo n.º 3
0
        public ActionResult Index()
        {
            phonebookEntities db = new phonebookEntities();
            // select c.*,g.* from ContactInfo c, Left Join GroupInfo g on (c.GroupID=g.GroupID)  where c.IsDelete=0
            var list = db.ContactInfoes.Include("GroupInfo").Where(c => c.IsDelete == 0 && c.GroupId > 0).ToList();

            return(View(list));
        }
Ejemplo n.º 4
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());
            }
        }
Ejemplo n.º 5
0
        private void SetGlist()
        {
            phonebookEntities db = new phonebookEntities();

            var        glist = db.GroupInfoes.ToList();
            SelectList slist = new SelectList(glist, "GroupId", "GroupName");

            ViewBag.glist = slist;
        }
Ejemplo n.º 6
0
        public ActionResult Add()
        {
            phonebookEntities db = new phonebookEntities();

            //2.0 获取grouinfo的所有数据以ViewBag.glist
            ViewBag.glist = db.GroupInfoes.ToList();

            return(View());
        }
Ejemplo n.º 7
0
        public ActionResult Add()
        {
            phonebookEntities db = new phonebookEntities();
            var        glist     = db.GroupInfoes.ToList();
            SelectList slist     = new SelectList(glist, "GroupId", "GroupName");

            ViewBag.glist = slist;
            return(View());
        }
Ejemplo n.º 8
0
        public ActionResult Index(FormCollection form)
        {
            //1.0 获取联系人表的所有数据
            phonebookEntities db = new phonebookEntities();
            var list             = db.ContactInfoes.Include("GroupInfo").Where(c => c.IsDelete == 0 && c.GroupId > 0)
                                   .Select(c => new{
                c.ContactName,
                c.CommonMobile,
                c.ID,
                c.GroupInfo.GroupName
            }).ToList();

            //2.0 将联系人的数据集合以 {status:0,msg:"",datas:[{},{}]}
            return(Json(new { status = 0, msg = "ok", datas = list }));
        }
Ejemplo n.º 9
0
        public ActionResult Delete(int id)
        {
            //1.0 物理删除
            phonebookEntities db    = new phonebookEntities();
            ContactInfo       model = new ContactInfo()
            {
                ID = id
            };

            db.ContactInfoes.Attach(model);
            db.ContactInfoes.Remove(model);
            db.SaveChanges();

            //2.0 返回json格式字符串给ajax异步对象
            return(Json(new { status = 0, msg = "删除成功" }));
        }
Ejemplo n.º 10
0
        public ActionResult Add(ContactInfo model)
        {
            //1.0 补全model中没有赋值,但是数据库中不允许有null值的字段
            model.ContactId = "1";
            model.Account   = "123";

            //2.0 实例化EF上下文对象
            phonebookEntities db = new phonebookEntities();

            //2.0.1 将model追加到EF容器中,并且将状态修改成added
            db.ContactInfoes.Add(model);

            //2.0.2 统一保存
            db.SaveChanges();

            return(Content("<script>alert('新增成功');window.location='/CInfo/Index'</script>"));
        }
Ejemplo n.º 11
0
        public ActionResult Edit(string id)
        {
            //合法性验证
            if (id.IsInt() == false || id == null)
            {
                return(Content("<script>alert('数据不存在或编辑错误');window.location='/CInfo/Index'</script>"));
            }

            //1.0 实例化EF上下文
            phonebookEntities db = new phonebookEntities();

            //2.0 获取grouinfo的所有数据以ViewBag.glist
            ViewBag.glist = db.GroupInfoes.ToList();

            //3.0
            int iid   = id.AsInt();
            var model = db.ContactInfoes.FirstOrDefault(c => c.ID == iid);

            return(View(model));
        }
Ejemplo n.º 12
0
        public ActionResult Edit(int id)
        {
            phonebookEntities db = new phonebookEntities();
            //1.0 根据id加载老数据实体
            var model = db.ContactInfoes.Where(c => c.ID == id)
                        .Select(c =>
                                new ContactInfoView()
            {
                ContactName = c.ContactName
                ,
                CommonMobile = c.CommonMobile
                ,
                GroupId = c.GroupId
            }).FirstOrDefault();


            //2.0 将GroupInfo中的所有数据以SelectList的形式通过ViewBag.glist 传入edit.cshtml
            SetGlist();

            return(View(model));
        }
Ejemplo n.º 13
0
        //
        // GET: /CInfo/

        public ActionResult Index()
        {
            //1.0
            phonebookEntities db = new phonebookEntities();
            var list             = db.ContactInfoes
                                   .Include("GroupInfo")
                                   .Where(c => c.IsDelete == 0 && c.GroupId > 0)
                                   .Select(c => new ContactInfoView()
            {
                ID           = c.ID,
                ContactName  = c.ContactName,
                CommonMobile = c.CommonMobile
                ,
                GroupInfo = new GroupInfoView()
                {
                    GroupName = c.GroupInfo.GroupName
                }
            }).ToList();

            return(View(list));
        }
Ejemplo n.º 14
0
        public ActionResult Add(ContactInfoView model)
        {
            System.Threading.Thread.Sleep(2000);        //实际项目中不能这么做
            //1.0 验证实体属性的值的合法性
            if (ModelState.IsValid == false)
            {
                //PhoneBookEntities db = new PhoneBookEntities();
                //var glist = db.GroupInfo.ToList();
                //SelectList slist = new SelectList(glist, "GroupId", "GroupName");
                //ViewBag.glist = slist;
                //return View();
                return(Json(new { status = 1, msg = "实体属性值的合法性验证失败" }));
            }

            //2.0
            try
            {
                ContactInfo entity = new ContactInfo()
                {
                    Account      = "1",
                    ContactId    = "1",
                    CommonMobile = model.CommonMobile,
                    ContactName  = model.ContactName,
                    GroupId      = model.GroupId,
                    IsDelete     = 0
                };

                phonebookEntities db = new phonebookEntities();
                db.ContactInfoes.Add(entity); //1、将entity追加到EF容器中, 2、修改状态类的状态为added
                db.SaveChanges();

                return(Json(new { status = 0, msg = "新增成功" }));
            }
            catch (Exception ex)
            {
                return(Json(new { status = 1, msg = ex.Message }));
            }
        }
Ejemplo n.º 15
0
        public ActionResult delete(string id)
        {
            //1.0 id的合法性验证
            if (id.IsInt() == false || id == null)
            {
                return(Content("<script>alert('数据不存在或其他错误');window.location='/CInfo/Index'</script>"));
            }

            //2.0 将id对应的数据进行物理删除
            ContactInfo model = new ContactInfo()
            {
                ID = id.AsInt()
            };

            phonebookEntities db = new phonebookEntities();

            db.ContactInfoes.Attach(model);
            db.ContactInfoes.Remove(model);

            db.SaveChanges();

            return(Content("<script>alert('删除成功');window.location='/CInfo/Index'</script>"));
        }