[HttpPost]//接收USER輸入的新增資料 public ActionResult Insert() { Categories _category = new Categories(); _category.CategoryName = Request.Form["CategoryName"]; _category.Description = Request.Form["Description"]; db.Entry(_category).State = EntityState.Added; db.SaveChanges(); return(RedirectToAction("Index")); }
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)); }
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()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public ActionResult Update(Customers willUpdated, bool isUpdate) { if (!string.IsNullOrEmpty(willUpdated.CustomerID)) { var updatedEntity = dbModel.Entry(willUpdated); updatedEntity.State = System.Data.Entity.EntityState.Modified; dbModel.SaveChanges(); return(RedirectToAction("Detail", new { userID = willUpdated.CustomerID, isUpdate = true })); } else { var added = dbModel.Entry(willUpdated); added.State = System.Data.Entity.EntityState.Added; dbModel.SaveChanges(); return(RedirectToAction("Detail", new { userID = added.Entity.CustomerID, isUpdate = false })); } }
public ActionResult Edit([Bind(Include = "CustomerID,CompanyName,ContactName,ContactTitle,Address,City,Region,PostalCode,Country,Phone,Fax")] Customer customer) { if (ModelState.IsValid) { db.Entry(customer).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(customer)); }
public ActionResult Edit([Bind(Include = "ProductID,ProductName,SupplierID,CategoryID,QuantityPerUnit,UnitPrice,UnitsInStock,UnitsOnOrder,ReorderLevel,Discontinued")] Product product) { if (ModelState.IsValid) { db.Entry(product).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.CategoryID = new SelectList(db.Categories, "CategoryID", "CategoryName", product.CategoryID); ViewBag.SupplierID = new SelectList(db.Suppliers, "SupplierID", "CompanyName", product.SupplierID); return(View(product)); }
public ActionResult Edit([Bind(Include = "OrderID,CustomerID,EmployeeID,OrderDate,RequiredDate,ShippedDate,ShipVia,Freight,ShipName,ShipAddress,ShipCity,ShipRegion,ShipPostalCode,ShipCountry")] Order order) { if (ModelState.IsValid) { db.Entry(order).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.CustomerID = new SelectList(db.Customers, "CustomerID", "CompanyName", order.CustomerID); ViewBag.EmployeeID = new SelectList(db.Employees, "EmployeeID", "LastName", order.EmployeeID); ViewBag.ShipVia = new SelectList(db.Shippers, "ShipperID", "CompanyName", order.ShipVia); return(View(order)); }
public IHttpActionResult PutProducts(int id, Products viewModel) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != viewModel.ProductID) { return(BadRequest()); } //db.Entry(viewModel).State = EntityState.Modified; var produto = db.Products.Find(); db.Entry(produto).CurrentValues.SetValues(viewModel); try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!ProductsExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }