コード例 #1
0
 public ActionResult Create(Fruit fruit)
 {
      using (FarmstoreEntities dbContext = new FarmstoreEntities())
     {
         
         dbContext.Fruits.Attach(fruit);
         dbContext.Entry(fruit).State = System.Data.Entity.EntityState.Added;
         dbContext.SaveChanges();
     }
     return RedirectToAction("Index", new { pageNumber = 1 });  
 }
コード例 #2
0
        public ActionResult Edit(Fruit fruit)
        {

            using (FarmstoreEntities dbContext = new FarmstoreEntities())
            {
                if (this.Request.Files != null && this.Request.Files.Count > 0 &&  //4 for upload file
                    this.Request.Files[0].ContentLength > 0 &&
                    this.Request.Files[0].ContentLength < 1024 * 120)  //file size less than 120K
                {

                    string fileName = Path.GetFileName(this.Request.Files[0].FileName);
                    string pathOfWebsite = "~/Images/Fruits/" + fileName;
                    fruit.FruitImagePath = pathOfWebsite;
                    this.Request.Files[0].SaveAs(this.Server.MapPath(pathOfWebsite));

                }
               
                    dbContext.Fruits.Attach(fruit);
                    dbContext.Entry(fruit).State = System.Data.Entity.EntityState.Modified;
                    dbContext.SaveChanges();
               
            }
            return RedirectToAction("Index", new { pageNumber = 1 });  //4 for paging
        }
コード例 #3
0
 public ActionResult Delete(int id)
 {
     Fruit fruit = null;
     using (FarmstoreEntities dbContext = new FarmstoreEntities())
     {
         fruit = dbContext.Fruits.SingleOrDefault(f => f.Id == id);
         dbContext.Fruits.Remove(fruit);
         dbContext.SaveChanges();
     }
     return RedirectToAction("Index", new { pageNumber = 1 });
 }