首页 > 代码库 > MVC3和MVC4中CRUD操作

MVC3和MVC4中CRUD操作

MVC3中EF实现的CRUD操作

 public class HomeController : Controller
    {
        //
        // GET: /Home/
        CarModelContainer db = new CarModelContainer();
       
        #region 查询所有 +Index()
        public ActionResult Index()
        {

            List<CarModel> list = (from c in db.CarModel select c).ToList();
            //ViewData["DataList"] = list;  上下两种效果一样  都是为了传递数据到前台
            return View(list);   //这样传输的是强类型的数据 在前台通过Model获取
        } 
        #endregion

        #region 添加信息 +Create()
       
        [HttpGet]
        public ActionResult Create()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Create(CarModel car)
        {
            if (ModelState.IsValid)   //后台的验证信息
            {
                db.CarModel.AddObject(car);
                db.SaveChanges();
                return RedirectToAction("Index");

            }
            else
            {
                return View(car);
            }

        } 
        #endregion

        #region 编辑信息 +Edit()
        [HttpGet]
        public ActionResult Edit(int id)
        {
            CarModel car=(from a in db.CarModel 
                          where a.ID==id 
                          select a).FirstOrDefault();

            if (car==null)
            {
                return RedirectToAction("Index");
                
            }
            return View(car);
        }
        [HttpPost]
        public ActionResult Edit(CarModel car)
        {
            try
            {   
                //第一种方式  使用LinQ查询出要编辑的对象
                //CarModel model=(from a in db.CarModel 
                //          where a.ID==car.ID 
                //          select a).FirstOrDefault();
                
                //第二种方式  创建一个所以要添加的对象 car中的实体属性 系统会自动检索得到对应的值
                CarModel model = new CarModel() { ID=car.ID};
                db.CarModel.Attach(model);

                UpdateModel(model);
                db.SaveChanges();
                return RedirectToAction("index");
            }
            catch (Exception )
            {
                ModelState.AddModelError("","修改失败,请查看详细错误信息");  
              
            }
            return View(car);
 
        }


        #endregion

        #region 删除信息 +Delete()
        public ActionResult Delete(int id)
        {
            CarModel car = (from a in db.CarModel
                            where a.ID == id
                            select a).FirstOrDefault();
            db.CarModel.DeleteObject(car);
            db.SaveChanges();
            return RedirectToAction("index");
        }
        #endregion


    }

MVC 4中EF实现的CRUD操作

 //添加实体
        public bool AddEntity(Model.Customer entity)
        {
            Hotel.Model.HotelModelContainer db = new Model.HotelModelContainer();
            db.Customer.Add(entity);
            int count = db.SaveChanges();
            if (count > 0)
            {
                return true;
            }
            return false;

        }
        //删除实体
        public bool DeleteEntity(Model.Customer entity)
        {
            Hotel.Model.HotelModelContainer db = new Model.HotelModelContainer();
            Customer cus = db.Customer.Where<Customer>(c => c.ID == entity.ID).FirstOrDefault<Customer>();
            if (cus != null)
            {
                db.Customer.Remove(cus);

            }
            int count = db.SaveChanges();
            if (count > 0)
            {
                return true;
            }
            return false;
        }
        //更新实体
        public bool UpdateEntity(Model.Customer entity)
        {
            Hotel.Model.HotelModelContainer db = new Model.HotelModelContainer();
            // DbEntityEntry<Customer> entry = db.Entry<Customer>(entity);
            //entry.State = System.Data.EntityState.Unchanged;
            //entry.Property("CustomerName").IsModified = true;
            // entry.Property("Phone").IsModified = true;</span>
            db.Entry(entity).State = EntityState.Modified;
            int count = db.SaveChanges();
            if (count > 0)
            {
                return true;
            }
            return false;

        }
       
        //查询数据
        public IQueryable<Model.Customer> LoadEntities(Func<Model.Customer, bool> whereLambda)
        {
            HotelModelContainer db = new HotelModelContainer();
            return db.Customer.Where<Customer>(whereLambda).AsQueryable<Customer>();
        }
        //查询分页数据并排序
        public IQueryable<Customer> LoadPageEntities<s>(Func<Customer, bool> whereLambda, int pageSize, int pageIndex, out int totalCount, Func<Customer, s> orderByLambda)
        {
            HotelModelContainer db = new HotelModelContainer();
            totalCount = db.Customer.Where<Customer>(whereLambda).Count();
            IQueryable<Customer> customers = db.Customer.Where<Customer>(whereLambda)
                .OrderBy<Customer, s>(orderByLambda)
                .Skip<Customer>(pageSize * (pageIndex - 1))
                .Take<Customer>(pageSize)
                .AsQueryable<Customer>();
            return customers;
        }
        //按照编号删除
        public bool DeleteEntity(object Id)
        {
            Hotel.Model.HotelModelContainer db = new Model.HotelModelContainer();
            Customer cus = db.Customer.Where<Customer>(c => c.ID == (int)Id).FirstOrDefault<Customer>();
            if (cus != null)
            {
                db.Customer.Remove(cus);

            }
            int count = db.SaveChanges();
            if (count > 0)
            {
                return true;
            }
            return false;
        }