Пример #1
0
        public ActionResult MultBinding(Product data1, [Bind(Prefix ="data3")]Product data2)
        {
            ViewBag.data1 = data1;
            ViewBag.data2 = data2;

            return View();
        }
Пример #2
0
        public ActionResult MultiBinding(Product data1, Product data2)
        {
            ViewBag.data1 = data1;
            ViewBag.data2 = data2;

            return View();
        }
Пример #3
0
        public ActionResult MutiBinding(Product data1,
			[Bind(Prefix = "data2", Exclude = "Active")]
			Product data2)
        {
            //Linhui JsonResult 預設只能使用 HttpPost,如欲使用HttpGet 須加上 JsonRequestBehavior.AllowGet宣告
            //return Json(data1, JsonRequestBehavior.AllowGet);
            ViewBag.data1 = data1;
            ViewBag.data2 = data2;
            //return View();
            return Json(data2);
        }
Пример #4
0
        public int AddProduct()
        {
            var product = new Product()
            {
                ProductName = "T-Shirt",
                Price = 199,
                Active = true,
                Stock = 10
            };

            db.Product.Add(product);

            db.SaveChanges();

            return product.ProductId;
        }
        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();
            }
        }
Пример #6
0
        public string TestInsert()
        {
            var db = new FabricsEntities();

            var product = new Product()
            {
                ProductName = "Entity",
                Price = 99,
                Stock = 10,
                Active = true
            };

            db.Product.Add(product);

            repo.UnitOfWork.Commit();

            return "OK: " + product.ProductId;
        }
Пример #7
0
 public ActionResult FormBinding(Product data)
 {
     return Json(data);
 }
Пример #8
0
        public ActionResult NewProduct(Product product)
        {
            if (ModelState.IsValid)
            {
                var prod = new Product();

                prod.ProductName = product.ProductName;
                prod.Price = product.Price;
                prod.Stock = 1;
                prod.Active = true;

                db.Product.Add(prod);
                try
                {
                    db.SaveChanges();
                }
                catch (DbEntityValidationException ex)
                {
                    //throw ex;
                    var allErrors = new List<string>();

                    foreach (DbEntityValidationResult re in ex.EntityValidationErrors)
                    {
                        foreach (DbValidationError err in re.ValidationErrors)
                        {
                            allErrors.Add(err.ErrorMessage);
                        }
                    }

                    ViewBag.Errors = allErrors;
                }

                return RedirectToAction("Index");
            }
            return View(product);
        }
Пример #9
0
 public ActionResult MultiBinding(Product data1, Product data2)
 {
     return Json(data1);
 }
Пример #10
0
 public ActionResult MultiBinding(Product data1,
     [Bind(Exclude="不需要綁訂的欄位",Include="想要綁訂的欄位",Prefix="網頁上的前置詞(預設為這裡原本的參數名,可以透過這個屬性更改跟這裡參數名稱不同)")]
     Product data2)
 {
     return Json(data1);
 }
Пример #11
0
        public ActionResult NewProduct(NewProductVM product)
        {
            if (ModelState.IsValid)//資料驗證
            {
                var pd = new Product();

                // var customerInput = Mapper.Map<Product>(product);

                pd.ProductName = product.ProductName;
                pd.Price = product.Price;
                pd.Active = true;
                pd.Stock = 0;
                //db.Product.Add(pd);

                repo.Add(pd);
                //Linhui 取得所有SaveChanges Error
                try
                {
                    // db.SaveChanges();
                    repo.UnitOfWork.Commit();
                    //Linhui 當Save Change後 Product 物件自動會取得ProductID
                }
                catch (DbEntityValidationException ex)//Linhui 不能只使用Exception
                {
                    //throw ex;
                    var allErrors = new List<string>();

                    foreach (DbEntityValidationResult re in ex.EntityValidationErrors)
                    {
                        foreach (DbValidationError err in re.ValidationErrors)
                        {
                            allErrors.Add(err.ErrorMessage);
                        }
                    }

                    //ViewBag.Errors = allErrors;
                    return Content(string.Join(",", allErrors.ToArray()));
                }

                //db.SaveChanges();
                return RedirectToAction("Index");//重新導向到 Index

            }
            return View();
        }
Пример #12
0
        public string TestInsert()
        {
            var db = new FabricsEntities();
            var prod = new Product()
            {
                ProductName = "EF",
                Price = 99,
                Stock = 5,
                Active = true
            };
            db.Product.Add(prod);

            db.SaveChanges();

            return "OK: "+prod.ProductId;
        }