예제 #1
0
 public ActionResult InsertUpdateCategory(string txtAd, string txtAciklama, int?hdnKatID)
 {
     if (hdnKatID != null)
     {
         Category cselected = DB.Categories.SingleOrDefault(x => x.CategoryID == hdnKatID);
         cselected.CategoryName = txtAd;
         cselected.Description  = txtAciklama;
         DB.Categories.Attach(cselected);                                     //Entity için update işlemi yapan method attach
         DB.Entry(cselected).State = System.Data.Entity.EntityState.Modified; //ben entitye dokunulduğunu söyliycem-Modified diyince tabloda dokunulmuş veri varmı karşılaştıracak ona göre işlem yapıcak.
         DB.SaveChanges();
     }
     else
     {
         Category c = new Category()
         {
             CategoryName = txtAd,
             Description  = txtAciklama
         };
         DB.Categories.Add(c);
         DB.SaveChanges();
     }
     return(RedirectToAction("AllCategories", "Category"));
     //hangi actionın hangi controlını çağırmak istiyorsun. //AllCategories action result'ını  Category controllerından çalıştırmak istiyorum diyorum.
     //return View();//Returnde view dönerse sistem patlar çünkü yeni bir kategory kaydettim ve o kategory'i çalıştıramıyoruz eski view de kaldı
     //HER ActionResult view döndürmek zorunda değil.
 }
예제 #2
0
        public IHttpActionResult PutTerritory(string id, Territory territory)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

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

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

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

            return(StatusCode(HttpStatusCode.NoContent));
        }
        public IHttpActionResult PutShippers(int id, Shippers shippers)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != shippers.ShipperID)
            {
                return(BadRequest());
            }

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

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

            return(StatusCode(HttpStatusCode.NoContent));
        }
        public async Task <IHttpActionResult> Put([FromODataUri] int key, [FromBody] Product entity)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            else if (key != entity.ProductID)
            {
                return(BadRequest("The key from the url must match the key of the entity in the body"));
            }

            var originalProduct = await db.Products.FindAsync(key);

            if (originalProduct == null)
            {
                return(NotFound());
            }
            else
            {
                db.Entry(originalProduct).CurrentValues.SetValues(entity);
                await db.SaveChangesAsync();
            }

            return(Updated(entity));
        }
예제 #5
0
        // PUT api/Categories/5
        public IHttpActionResult PutCategory(int id, Category category)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != category.CategoryID)
            {
                return(BadRequest());
            }

            _db.Entry(category).State = EntityState.Modified;

            try
            {
                _db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CategoryExists(id))
                {
                    return(NotFound());
                }

                throw;
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
        // PUT: odata/Employees(5)
        public IHttpActionResult Put([FromODataUri] int key, Employee employee)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (key != employee.EmployeeID)
            {
                return(BadRequest());
            }

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

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

            return(Updated(employee));
        }
예제 #7
0
        public ActionResult Products_Update([DataSourceRequest] DataSourceRequest request, Product product)
        {
            if (ModelState.IsValid)
            {
                var entity = new Product
                {
                    ProductID       = product.ProductID,
                    ProductName     = product.ProductName,
                    SupplierID      = product.SupplierID,
                    CategoryID      = product.CategoryID,
                    QuantityPerUnit = product.QuantityPerUnit,
                    UnitPrice       = product.UnitPrice,
                    UnitsInStock    = product.UnitsInStock,
                    UnitsOnOrder    = product.UnitsOnOrder,
                    ReorderLevel    = product.ReorderLevel,
                    Discontinued    = product.Discontinued
                };

                db.Products.Attach(entity);
                db.Entry(entity).State = EntityState.Modified;
                db.SaveChanges();
            }

            return(Json(new[] { product }.ToDataSourceResult(request, ModelState)));
        }
        // PUT odata/Products(5)
        public IHttpActionResult Put([FromODataUri] int key, Product product)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (key != product.ProductID)
            {
                return(BadRequest());
            }

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

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

            return(Updated(product));
        }
예제 #9
0
        public IHttpActionResult PutOrder_Detail(int id, Order_Detail order_Detail)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != order_Detail.OrderID)
            {
                return(BadRequest());
            }

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

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

            return(StatusCode(HttpStatusCode.NoContent));
        }
예제 #10
0
        public IHttpActionResult PutRichard(int id, Richard richard)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != richard.ID)
            {
                return(BadRequest());
            }

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

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

            return(StatusCode(HttpStatusCode.NoContent));
        }
예제 #11
0
        // PUT api/Products/5
        public IHttpActionResult PutProduct(int id, Product product)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != product.ProductID)
            {
                return(BadRequest());
            }

            _db.Entry(product).State = EntityState.Modified;

            try
            {
                _db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ProductExists(id))
                {
                    return(NotFound());
                }

                throw;
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
예제 #12
0
        // PUT api/Employee/5
        public HttpResponseMessage PutEmployee(int id, Employee employee)
        {
            if (!ModelState.IsValid)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
            }

            if (id != employee.EmployeeID)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest));
            }

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

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.NotFound, ex));
            }

            return(Request.CreateResponse(HttpStatusCode.OK));
        }
예제 #13
0
        public IHttpActionResult PutEmployee(int id, Employee employee)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != employee.EmployeeID)
            {
                return(BadRequest());
            }

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

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

            return(StatusCode(HttpStatusCode.NoContent));
        }
        public IHttpActionResult PutCustomer(string id, Customer customer)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != customer.CustomerID)
            {
                return(BadRequest());
            }

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

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

            return(StatusCode(HttpStatusCode.NoContent));
        }
        // PUT: odata/Territories(5)
        public IHttpActionResult Put([FromODataUri] string key, Territory territory)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (key != territory.TerritoryID)
            {
                return(BadRequest());
            }

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

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

            return(Updated(territory));
        }
        public ActionResult Editar(EmployeeViewModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    using (NorthwindEntities db = new NorthwindEntities())
                    {
                        var oEmployee = db.Employees.Find(model.EmployeeID);

                        oEmployee.FirstName  = model.FirstName;
                        oEmployee.LastName   = model.LastName;
                        oEmployee.Title      = model.Title;
                        oEmployee.Address    = model.Address;
                        oEmployee.EmployeeID = model.EmployeeID;

                        db.Entry(oEmployee).State = System.Data.Entity.EntityState.Modified;
                        db.SaveChanges();
                    }

                    return(Redirect("~/Employees/"));
                }

                return(View(model));
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
예제 #17
0
        static void Edit2()
        {
            Customers _Customers = new Customers()
            {
                CustomerID  = "A001",
                Address     = "南山區",
                City        = "基隆",
                Phone       = "11111111",
                CompanyName = "台北電商",
                ContactName = "Eddie"
            };

            using (NorthwindEntities db = new NorthwindEntities())
            {
                //物件加入EF對象裡面
                DbEntityEntry <Customers> entry = db.Entry <Customers>(_Customers);
                entry.State = System.Data.Entity.EntityState.Unchanged;
                entry.Property("ContactName").IsModified = true;

                //var u = db.Customers.Attach(_Customers);
                //u.ContactName = "郭";
                db.SaveChanges();
                Console.WriteLine("修改成功:");
                Console.WriteLine(_Customers.ContactName);
            }
        }
예제 #18
0
        // PUT: odata/Shippers(5)
        public IHttpActionResult Put([FromODataUri] int key, Shipper shipper)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (key != shipper.ShipperID)
            {
                return(BadRequest());
            }

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

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

            return(Updated(shipper));
        }
예제 #19
0
        public ActionResult Edit(/*[Bind(Include = "ProductID,ProductName,SupplierID,CategoryID,QuantityPerUnit,UnitPrice,UnitsInStock,UnitsOnOrder,ReorderLevel,Discontinued")] Product product*/
            FormCollection form)
        {
            Product product = new Product();

            //使用 TryUpdateModel(或 UpdateModel 也可以)做出 Model Binding 功能([Bind(Include = "{column1}")] T model)
            if (TryUpdateModel(product,
                               "",
                               form.AllKeys,                                                                                              // Include Properties
                               new[] { "SupplierID,CategoryID,UnitPrice,UnitsInStock,UnitsOnOrder,ReorderLevel" }) && ModelState.IsValid) //exclude Properties
            {
                db.Entry(product).State = EntityState.Modified;
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            else
            {
                //驗證失敗,錯誤處理
                return(View(product));
            }
            #region 基架自動產生的程式碼

            /*
             * if (ModelState.IsValid)
             * {
             *  db.Entry(product).State = EntityState.Modified;
             *  db.SaveChanges();
             *  return RedirectToAction("Index");
             * }
             * return View(product);
             */
            #endregion
        }
예제 #20
0
 public ActionResult Products_Update([DataSourceRequest] DataSourceRequest request, ProductViewModel product)
 {
     if (ModelState.IsValid)
     {
         using (var northwind = new NorthwindEntities())
         {
             // Create a new Product entity and set its properties from the posted ProductViewModel.
             var entity = new Product
             {
                 ProductID    = product.ProductID,
                 ProductName  = product.ProductName,
                 UnitsInStock = product.UnitsInStock
             };
             // Attach the entity.
             northwind.Products.Attach(entity);
             // Change its state to Modified so Entity Framework can update the existing product instead of creating a new one.
             northwind.Entry(entity).State = EntityState.Modified;
             // Or use ObjectStateManager if using a previous version of Entity Framework.
             // northwind.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified);
             // Update the entity in the database.
             northwind.SaveChanges();
         }
     }
     // Return the updated product. Also return any validation errors.
     return(Json(new[] { product }.ToDataSourceResult(request, ModelState)));
 }
예제 #21
0
        public void Update(T item)
        {
            T             data  = GetById(item.Id);
            DbEntityEntry entry = db.Entry(data);

            entry.CurrentValues.SetValues(item);
            Save();
        }
예제 #22
0
        public ActionResult UpdateCategory(Category item)
        {
            Category guncellenecek = db.Categories.Find(item.CategoryID);

            db.Entry(guncellenecek).CurrentValues.SetValues(item);
            db.SaveChanges();
            return(RedirectToAction("CategoryList"));
        }
        public ActionResult UpdateProduct([Bind(Prefix = "Item2")] Product item)
        {
            Product ToBeUpdated = db.Products.Find(item.ProductID);

            db.Entry(ToBeUpdated).CurrentValues.SetValues(item);
            db.SaveChanges();
            return(RedirectToAction("ListProducts", "ListProducts"));
        }
예제 #24
0
        public ActionResult UpdateProduct(Product item)
        {
            Product guncellenecek = db.Products.Find(item.ProductID);

            db.Entry(guncellenecek).CurrentValues.SetValues(item);
            db.SaveChanges();
            return(RedirectToAction("ProductList"));
        }
예제 #25
0
파일: DAO.cs 프로젝트: 2She2/DataBases
 public static void Delete(Customer customer)
 {
     using (var db = new NorthwindEntities())
     {
         var entry = db.Entry(customer);
         entry.State = EntityState.Deleted;
         db.SaveChanges();
     }
 }
예제 #26
0
        public ActionResult Sil(int id, bool d = true)
        {
            Categories secCat = db.Categories.Find(id);

            //db.Categories.Remove(secCat);
            db.Entry(secCat).State = EntityState.Deleted;
            db.SaveChanges();
            return(RedirectToAction("Liste"));
        }
예제 #27
0
        public ActionResult Edit(Personne p)
        {
            //*******************Version-1*************************
            //new PersonneDAO().addOrUpdatePersonne(p);
            //PersonneDaoDisco.addOrUpdatePersonne(p);
            //Personne pers= entities.Personnes.First(pe => pe.Id == p.Id );
            //pers.Nom = p.Nom;....ou bien

            //*******************Version-2*************************
            //entities.Entry(pers).CurrentValues.SetValues(p);
            //entities.SaveChanges();.... ou bien

            //*******************Version-3*************************
            entities.Personnes.Attach(p);
            entities.Entry(p).State = EntityState.Modified;
            entities.SaveChanges();
            return(Redirect("/"));
        }
예제 #28
0
        public ActionResult Edit(Category category)
        {
            //Category currentCategory = _dbContext
            //    .Categories
            //    .SingleOrDefault(c => c.CategoryID == category.CategoryID);

            //currentCategory.CategoryName = category.CategoryName;
            //currentCategory.Description = category.Description;


            _dbContext.Entry(category).State = System.Data.Entity.EntityState.Modified;
            _dbContext.Entry(category).Property("Picture").IsModified = false;

            _dbContext.SaveChanges();


            return(RedirectToAction("Index"));
        }
예제 #29
0
 public ActionResult Update(Shipper shipper)
 {
     //Shipper updated= db.Shippers.Find(shipper.ShipperID);
     // updated.CompanyName = shipper.CompanyName;
     db.Entry(shipper).State = System.Data.Entity.EntityState.Modified;
     db.SaveChanges();
     TempData["success"] = $"{shipper.CompanyName} başarılı bir şekilde güncellendi";
     return(RedirectToAction("Index"));
 }
예제 #30
0
        public IHttpActionResult Post(Product product)
        {
            Product addedProduct = new Product()
            {
                ProductID    = product.ProductID,
                CategoryID   = product.CategoryID,
                ProductName  = product.ProductName,
                UnitPrice    = product.UnitPrice,
                UnitsInStock = product.UnitsInStock
            };

            _db.Entry <Product>(addedProduct).State = System.Data.Entity.EntityState.Added;
            if (_db.SaveChanges() > 0)
            {
                return(Ok());
            }
            return(BadRequest());
        }
예제 #31
0
 private async Task ChangeEntityStateAsync(object entity, EntityState state)
 {
     using (var ctx = new NorthwindEntities())
     {
         var entry = ctx.Entry(entity);
         entry.State = state;
         await ctx.SaveChangesAsync();
     }
 }