示例#1
0
        public ActionResult CreateProduct(SanPham sp, HttpPostedFileBase linkanh)
        {
            ViewBag.danhmucsanpham = dmDao.li();
            ViewBag.loaisanpham    = loaiSPDao.dsLoaiSP();
            try
            {
                // lấy tên của hình ảnh
                var tenfile = Path.GetFileName(linkanh.FileName);
                // tạo đường dẫn
                var duongdan = Path.Combine(Server.MapPath("/Photo"), tenfile);
                if (System.IO.File.Exists(duongdan))
                {
                    sp.linkanh  = linkanh.FileName;
                    ViewBag.loi = "Hình ảnh đã tồn tại";
                }
                else
                {
                    linkanh.SaveAs(duongdan);
                    sp.linkanh = linkanh.FileName;
                }
            }
            catch (Exception)
            {
                ViewBag.loianh = "Chọn hình ảnh";
                return(View("Create"));
            }
            if (ModelState.IsValid)
            {
                SanPhamDao spDao = new SanPhamDao();

                spDao.Create(sp);
                return(Redirect("Index"));
            }

            return(View("Create"));
        }