public ActionResult Create(Product data) { if (ModelState.IsValid) { db.Product.Add(data); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(data)); }
public ActionResult Create([Bind(Include = "OrderId,ClientId,OrderDate,OrderTotal,OrderStatus")] Order order) { if (ModelState.IsValid) { db.Order.Add(order); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.ClientId = new SelectList(db.Client, "ClientId", "FirstName", order.ClientId); return(View(order)); }
public ActionResult Create([Bind(Include = "ClientId,FirstName,MiddleName,LastName,Gender,DateOfBirth,CreditRating,XCode,OccupationId,TelephoneNumber,Street1,Street2,City,ZipCode,Longitude,Latitude,Notes")] Client client) { if (ModelState.IsValid) { db.Client.Add(client); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.OccupationId = new SelectList(db.Occupation, "OccupationId", "OccupationName", client.OccupationId); return(View(client)); }
public ActionResult Create([Bind(Include = "OrderId,LineNumber,ProductId,Qty,LineTotal")] OrderLine orderLine) { if (ModelState.IsValid) { db.OrderLines.Add(orderLine); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.OrderId = new SelectList(db.Orders, "OrderId", "OrderStatus", orderLine.OrderId); ViewBag.ProductId = new SelectList(db.Products, "ProductId", "ProductName", orderLine.ProductId); return(View(orderLine)); }
public ActionResult Create() { var product = new Product() { ProductName = "White Cat", Active = true, Price = 100, Stock = 5 }; db.Product.Add(product); db.SaveChanges(); // 加上這行才會把變更真正存到資料庫 return(RedirectToAction("Index")); }
public ActionResult Create() { var product = new Product() { ProductName = "White 001", Price = 1000, Active = true, Stock = 5 }; db.Product.Add(product); db.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult CreateProduct() { var product = new Product() { ProductName = "Tercel", Active = true, Price = 1999, Stock = 5 }; db.Product.Add(product); db.SaveChanges(); return(View(product)); }
public ActionResult Create(FormCollection collection) { try { // TODO: Add insert logic here //Product c = new Product(); //產生一個Product物件 Product P = new Product(); //將上面的collection帶入就是傳進來的 collection["這是網頁上的name"] //型別的部分滑鼠移過去就可以看到要轉什麼型別 P.ProductName = Convert.ToString(collection["ProductName"]); P.Price = Convert.ToDecimal(collection["Price"]); P.Active = true; P.Stock = Convert.ToDecimal(collection["Stock"]); var db = new FabricsEntities(); db.Product.Add(P); db.SaveChanges(); return(RedirectToAction("Index")); } catch { return(View()); } }
// GET: CRUD/Delete/5 public ActionResult Delete(int id) { var db = new FabricsEntities(); var client = db.Client.Find(id); if (client != null) { foreach (var item in client.Order.ToList()) { db.OrderLine.RemoveRange(item.OrderLine.ToList()); } db.Order.RemoveRange(client.Order.ToList()); db.Client.Remove(client); db.SaveChanges(); return(RedirectToAction("Index")); } else { return(RedirectToAction("Index")); } }
public ActionResult Create([Bind(Include = "ProductId,ProductName,Price,Active,Stock")] Product product) { #region 產生 下拉選單 使用的資料,試改用 ActionFilter 傳入,因此下述語法不執行 //var price_list = (from p in db.Product // select new { // Value = p.Price, // Text = p.Price // }).Distinct().OrderBy(p => p.Value); //ViewBag.Price = new SelectList(price_list, "Value", "Text"); #endregion if (ModelState.IsValid) { db.Product.Add(product); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(product)); }
public ActionResult Create(FormCollection collection) { try { Product data = new Product() { ProductName = collection["ProductName"], Price = Convert.ToDecimal(collection["Price"]), Active = true, Stock = Convert.ToDecimal(collection["Stock"]) }; db.Product.Add(data); db.SaveChanges(); return(RedirectToAction("Index")); } catch { return(View()); } }
public ActionResult Create(FormCollection collection) { try { Product prod = new Product(); prod.ProductName = collection["ProductName"]; prod.Price = Convert.ToDecimal(collection["Price"]); prod.Stock = Convert.ToDecimal(collection["Stock"]); prod.Active = true; db.Product.Add(prod); db.SaveChanges(); return(RedirectToAction("Index")); } catch { return(View()); } }
public ActionResult Create(FormCollection collection) { try { Product newProd = new Product(); newProd.ProductName = collection["ProductName"]; newProd.Price = Convert.ToDecimal(collection["Price"]); newProd.Stock = Convert.ToDecimal(collection["Stock"]); newProd.Active = true; db.Products.Add(newProd); db.SaveChanges(); return(RedirectToAction("Index")); } catch (Exception ex) { ViewBag.Error = ex.Message; return(View()); } }
public ActionResult Create(FormCollection collection) { try { Product prod = new Product(); prod.ProductName = collection["ProductName"]; prod.Price = Convert.ToDecimal(collection["Price"]); prod.Active = true; prod.Stock = Convert.ToDecimal(collection["Stock"]); db.Product.Add(prod); db.SaveChanges(); // TODO: Add insert logic here return(RedirectToAction("Index")); } catch (DbEntityValidationException ex) { return(View()); } }
public ActionResult Create([Bind(Include = "ProductId,ProductName,Price,Active,Stock")] Product product) { if (ModelState.IsValid) { db.Product.Add(product); db.SaveChanges(); return(RedirectToAction("Index")); } var price_list = (from p in db.Product select new { Value = p.Price, Text = p.Price }).Distinct().OrderBy(p => p.Value); ViewBag.Price = new SelectList(price_list, "Value", "Text"); return(View(product)); }
public ActionResult BatchUpdate() { var db = new FabricsEntities(); var data = db.Product.Where(p => p.ProductName.StartsWith("C")); foreach (var item in data) { item.Price = item.Price * 2; } db.SaveChanges(); //return View(); return(RedirectToAction("Index")); }
private void SaveChanges() { try { db.SaveChanges(); } catch (DbEntityValidationException ex) { foreach (DbEntityValidationResult item in ex.EntityValidationErrors) { var entityName = item.Entry.GetType().Name; foreach (DbValidationError err in item.ValidationErrors) { throw new Exception(entityName + "類型驗證失敗 test" + err.ErrorMessage); } } throw; } }
public ActionResult BatchUpdate(MBProductsModel[] items) { if (ModelState.IsValid) { foreach (var o in items) { var product = db.Product.Find(o.ProductId); if (product != null) { product.ProductName = o.ProductName; product.Price = o.Price; product.Stock = o.Stock; product.ProductId = o.ProductId; } } db.SaveChanges(); return(RedirectToAction("ProductList")); } return(View()); }
public ActionResult BatchUpdate(ClientBatchUpdateVM[] items) { if (ModelState.IsValid) { foreach (var item in items) { var c = db.Client.Find(item.ClientId); c.FirstName = item.FirstName; c.MiddleName = item.MiddleName; c.LastName = item.LastName; } db.SaveChanges(); return(RedirectToAction("BatchUpdate")); } GetClients(); return(View()); }
public ActionResult BatchUpdate() { var db = new FabricsEntities(); var data = db.Product.Where(p => p.ProductName.StartsWith("A")); foreach (var item in data) { item.Price = item.Price * 2; } try { db.SaveChanges(); } catch (DbEntityValidationException ex) { throw ex; } return(RedirectToAction("Index")); }
// GET: CRUD/Delete/5 public ActionResult Delete(int id) { var db = new FabricsEntities(); //找到要刪除的id var client = db.Client.Find(id); //foreach先找到這資料表最後一個關聯性先找最後一筆做刪除 foreach (var order in client.Order.ToList()) { //找到order的關聯orderLine做刪除 db.OrderLine.RemoveRange(order.OrderLine); } //再將order中與這個client有關連到的做移除 db.Order.RemoveRange(client.Order.ToList()); //將選擇的id做移除 db.Client.Remove(client); //做交易處理正式嘗試移除 db.SaveChanges(); //return View(); return(RedirectToAction("Index")); }
public ActionResult Create(FormCollection collection) { try { var db = new FabricsEntities(); var product = new Product(); product.ProductName = collection["ProductName"]; product.Price = Convert.ToDecimal(collection["Price"]); product.Stock = Convert.ToDecimal(collection["Stock"]); product.Active = true; db.Product.Add(product); db.SaveChanges(); // TODO: Add insert logic here return(RedirectToAction("Index")); } catch { return(View()); } }
public ActionResult AddNewProduct(ProductViewModel data) { if (!ModelState.IsValid) { return(RedirectToAction("Index")); } var newData = new Product() { ProductId = data.ProductId, ProductName = data.ProductName, Active = true, Price = data.Price, Stock = data.Stock }; db.Product.Add(newData); db.SaveChanges(); return(RedirectToAction("NewIndex")); }