public virtual void Delete(TEntity entityToDelete)
 {
     if (context.Entry(entityToDelete).State == EntityState.Detached)
     {
         dbSet.Attach(entityToDelete);
     }
     dbSet.Remove(entityToDelete);
 }
Exemple #2
0
        public ActionResult Edit([Bind(Include = "ProductID,Name,ProductNumber,Color,StandardCost,ListPrice,Size,Weight,ProductCategoryID,ProductModelID,SellStartDate,SellEndDate,DiscontinuedDate,ThumbNailPhoto,ThumbnailPhotoFileName,rowguid,ModifiedDate")] Product product, HttpPostedFileBase uploadFile)
        {
            if (ModelState.IsValid)
            {
                product.ModifiedDate = System.DateTime.Now;
                product.rowguid      = Guid.NewGuid();

                // Manager uploaded an image
                if (uploadFile != null && uploadFile.ContentLength > 0)
                {
                    product.ThumbNailPhoto         = ConvertImage(uploadFile);
                    product.ThumbnailPhotoFileName = uploadFile.FileName;
                    db.Entry(product).State        = EntityState.Modified;
                }
                // Manager didn't upload anything
                else
                {
                    // Since the photos always turn null on edit, we need to fetch the product's details
                    var item = db.Products.SingleOrDefault(i => i.ProductID == product.ProductID);

                    // In case thumbnail photo was already null
                    if (item.ThumbNailPhoto == null)
                    {
                        // Get the byte array for no image
                        var image = db.Products.FirstOrDefault(i => i.ThumbnailPhotoFileName == "no_image_available_small.gif");
                        product.ThumbNailPhoto         = image.ThumbNailPhoto;
                        product.ThumbnailPhotoFileName = "no_image_available_small.gif";
                    }
                    else
                    {
                        product.ThumbNailPhoto         = item.ThumbNailPhoto;
                        product.ThumbnailPhotoFileName = item.ThumbnailPhotoFileName;
                    }

                    // We have to set the new values this way instead or else Visual Studios will get confused with "item" and "product" sharing the same ID
                    // It's weird but this works
                    db.Entry(item).CurrentValues.SetValues(product);
                }

                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            var category = (from x in db.ProductCategories where x.ParentProductCategoryID != 1 && x.ParentProductCategoryID != null select new SelectListItem {
                Value = x.ProductCategoryID.ToString(), Text = x.Name
            }).Distinct();

            ViewBag.ProductCategory = category;
            return(View(product));
        }
Exemple #3
0
        public IHttpActionResult PutSalesTerritory(int id, SalesTerritory salesTerritory)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != salesTerritory.TerritoryID)
            {
                return(BadRequest());
            }

            db.Entry(salesTerritory).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!SalesTerritoryExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
 public HttpResponseMessage Post(Employee employee)
 {
     try
     {
         using (var db = new AdventureWorks2012Entities())
         {
             db.Entry(employee).State        = EntityState.Modified;
             db.Entry(employee.Person).State = EntityState.Modified;
             db.SaveChanges();
         }
         return(Request.CreateResponse(HttpStatusCode.OK));
     }
     catch (Exception e) {
         return(Request.CreateResponse(HttpStatusCode.Conflict));
     }
 }
Exemple #5
0
 public ActionResult Edit([Bind(Include = "ProductID,Name,ProductNumber,Color,StandardCost,ListPrice,Size,Weight,ProductCategoryID,ProductModelID,SellStartDate,SellEndDate,DiscontinuedDate,ThumbNailPhoto,ThumbnailPhotoFileName,rowguid,ModifiedDate")] Product product)
 {
     if (ModelState.IsValid)
     {
         db.Entry(product).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.ProductCategoryID = new SelectList(db.ProductCategories, "ProductCategoryID", "Name", product.ProductCategoryID);
     return(View(product));
 }
Exemple #6
0
 public ActionResult Edit([Bind(Include = "ProductCategoryID,ParentProductCategoryID,Name,rowguid,ModifiedDate")] ProductCategory productCategory)
 {
     if (ModelState.IsValid)
     {
         db.Entry(productCategory).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.ParentProductCategoryID = new SelectList(db.ProductCategories, "ProductCategoryID", "Name", productCategory.ParentProductCategoryID);
     return(View(productCategory));
 }
 public ActionResult Edit([Bind(Include = "id,ProductID,Name,Rating,Review1")] Review review)
 {
     if (ModelState.IsValid)
     {
         db.Entry(review).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.ProductID = new SelectList(db.Products, "ProductID", "Name", review.ProductID);
     return(View(review));
 }
 public ActionResult Edit([Bind(Include = "BusinessEntityID,NationalIDNumber,LoginID,OrganizationLevel,JobTitle,BirthDate,MaritalStatus,Gender,HireDate,SalariedFlag,VacationHours,SickLeaveHours,CurrentFlag,rowguid,ModifiedDate")] Employee employee)
 {
     if (ModelState.IsValid)
     {
         db.Entry(employee).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.BusinessEntityID = new SelectList(db.People, "BusinessEntityID", "PersonType", employee.BusinessEntityID);
     ViewBag.BusinessEntityID = new SelectList(db.SalesPersons, "BusinessEntityID", "BusinessEntityID", employee.BusinessEntityID);
     return(View(employee));
 }
Exemple #9
0
        public PartialViewResult Edit(Product product)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var editedProduct = db.Products.Find(product.ProductID);
                    editedProduct.Color           = product.Color;
                    db.Entry(editedProduct).State = EntityState.Modified;
                    db.SaveChanges();
                    return(PartialView("_EditParital", editedProduct));
                }
            }
            catch (DataException dex)
            {
                //Log the error (uncomment dex variable name and add a line here to write a log.
                ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
            }
            var oldProduct = db.Products.Find(product.ProductID);

            return(PartialView("_EditPartial", oldProduct));
        }