码迷,mamicode.com
首页 > Web开发 > 详细

MVC3和MVC4中CRUD操作

时间:2014-07-11 00:57:01      阅读:277      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   使用   strong   数据   

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;
        }

MVC3和MVC4中CRUD操作,布布扣,bubuko.com

MVC3和MVC4中CRUD操作

标签:style   blog   http   使用   strong   数据   

原文地址:http://blog.csdn.net/syaguang2006/article/details/37603477

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!