asp.net mvc2,linq to entity的创建,编辑,删除

 说明:以下使用的OjbectContext为: StudentApartmentStoreEntities StudentApartmentDB = new StudentApartmentStoreEntities();

 

1.创建新一项

 

 

 [HttpPost]

        public ActionResult Create(FormCollection collection)

 

 

try

            {

 

                string NewApartName = collection["ApartName"].Trim();

                var checkapp = StudentApartmentDB.Apartment_Info.SingleOrDefault(item => item.ApartName == NewApartName);

                if (checkapp == null)

                {

 

                    checkapp = new Apartment_Info

                    {

                        ApartmentId = Guid.NewGuid(),

                        ApartName = NewApartName

                    };

                    StudentApartmentDB.AddToApartment_Info(checkapp);

                    StudentApartmentDB.SaveChanges();

                    return RedirectToAction("Index");

                }

                else

                {

 

 

                    return SendMessage("公寓名称已经存在", "公寓名称已经存在,请换一个名称。", true);

                }

            }

            catch

            {

                return View();

            }

 

 

try

            {

                // TODO: Add update logic here

                string NewApartName = collection["ApartName"].Trim();

                var checkapp = StudentApartmentDB.Apartment_Info.SingleOrDefault(item => item.ApartName == NewApartName);

                if (checkapp == null)

                {

 

                    checkapp = new Apartment_Info

                    {

                        ApartmentId = Guid.NewGuid(),

                        ApartName = NewApartName

                    };

                    StudentApartmentDB.AddToApartment_Info(checkapp);

                    StudentApartmentDB.SaveChanges();

                    return RedirectToAction("Index");

                }

                else

                {

 

 

                    return SendMessage("公寓名称已经存在", "公寓名称已经存在,请换一个名称。", true);

                }

            }

            catch

            {

                return View();

            }

 

2.编辑项:

 

public ActionResult Edit(Guid id)

        {

            return View();

        }

 

 

        [HttpPost]

        public ActionResult Edit(Guid id, FormCollection collection)

        {

 

            string NewApartName = collection["ApartName"].Trim();

            Apartment_Info updateApartent = new Apartment_Info

                    {

                        ApartmentId = id,

                        ApartName = NewApartName

 

                    };

            System.Data.EntityKey key = default(System.Data.EntityKey);

            object originalItem = null;

 

            using (StudentApartmentDB)

            {

                key = StudentApartmentDB.CreateEntityKey("Apartment_Info", updateApartent);

                if (StudentApartmentDB.TryGetObjectByKey(key, out originalItem))

                {

                    StudentApartmentDB.ApplyCurrentValues(key.EntitySetName, updateApartent);

                }

                try

                {

                    StudentApartmentDB.SaveChanges();

                    return RedirectToAction("Index");

                }

                catch

                {

                    return SendMessage("修改公寓失败", "修改公寓失败",true);

                }

            }

 

        }

2.删除项:
  public ActionResult Delete(Guid id)
        {
            var album = StudentApartmentDB.Apartment_Info.Single(a => a.ApartmentId == id);
            return View(album);
        }
        [HttpPost]
        public ActionResult Delete(Guid id, FormCollection collection)
        {
            try
            {
           
                var cartItem = StudentApartmentDB.Apartment_Info.Single(
             apart => apart.ApartmentId == id);
                if (cartItem != null)
                {
                    StudentApartmentDB.Apartment_Info.DeleteObject(cartItem);
                    StudentApartmentDB.SaveChanges();
                }
                return View("Deleted", cartItem);
            }
            catch
            {
                return SendMessage("删除失败", "删除失败!", true);
            }
        }

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值