예제 #1
0
        // GET: MB
        public ActionResult Index()
        {
            var data = new NewProductVM()
            {
                Price = 100,
                ProductName = "AAA"

            };
            ViewData["MyName"] = "Linhui";
            ViewBag.MyApp = "First1";
            ViewBag.Products = db.Product.Take(5).ToList();
            ViewData.Model = data;

            TempData["Msg"] = "This is TempData Msg"; // Linhui TempDate By Session

            return View(data);
        }
예제 #2
0
        // GET: MB
        public ActionResult Index()
        {
            var data = new NewProductVM()
            {
                Price = 100,
                ProductName = "T-Shirt"
            };

            ViewData["MyTitle"] = "will";
            ViewBag.MyTitle = "will 2";

            var db = new FabricsEntities();
            db.Configuration.LazyLoadingEnabled = false;
            ViewBag.Products = db.Product.Take(5).ToList();

            TempData["Msg"] = "TEST!";

            ViewData.Model = data;
            return View();
        }
예제 #3
0
        public ActionResult Index()
        {
            var data = new NewProductVM()
            {
                Price = 100,
                ProductName = "T-Shirt",
                ProductId = 5
            };

            ViewData["MyName"] = "Leo";

            ViewData["MyTitle"] = "ASP.NET MVC 1";
            ViewBag.MyTitle     = "ASP.NET MVC 2";

            ViewBag.Products = db.Product.Take(5).ToList();

            TempData["Msg"] = "test";

            ViewData.Model = data;

            return View();
        }
예제 #4
0
        public ActionResult NewProduct(NewProductVM product)
        {
            if (ModelState.IsValid)
            {
                var prod = Mapper.Map<Product>(product);

                //var prod = new Product();
                //prod.ProductName = product.ProductName;
                //prod.Price = product.Price;

                repo.Add(prod);

                try
                {
                    repo.UnitOfWork.Commit();
                }
                catch (DbEntityValidationException ex)
                {
                    var allErrors = new List<string>();

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

                    return Content(String.Join("<br>", allErrors.ToArray()));
                    //throw ex;
                }

                return RedirectToAction("Index");
            }

            return View(product);
        }
예제 #5
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();
        }
예제 #6
0
        public ActionResult NewProduct(NewProductVM product)
        {
            if (ModelState.IsValid)
            {
                //var prod = new Product();
                //prod.ProductName = product.ProductName;
                //prod.Price = product.ProductPrice;
                //prod.Stock = 10;
                //prod.Active = true;

                var prod = Mapper.Map<Product>(product);
                prod.Stock = 10;
                prod.Active = true;

                repo.Add(prod);
                try
                {
                    //db.SaveChanges();
                    repo.UnitOfWork.Commit();
                }
                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);
        }