public ActionResult Create(Models.Producer createPdc, HttpPostedFileBase file) { if (Session["accname"] == null) { Session["accname"] = null; return(RedirectToAction("Login", "Account")); } else { if (file != null) { if (file.ContentLength > 0) { try { string nameFile = Path.GetFileName(file.FileName); file.SaveAs(Path.Combine(Server.MapPath("/Image"), nameFile)); createPdc.pdcPhoto = "/Image/" + nameFile; } catch (Exception) { ViewBag.CreatePdcError = "Không thể chọn ảnh."; } } var pdc = dbPdc.Producers.SingleOrDefault(c => c.pdcName.Equals(createPdc.pdcName)); try { if (ModelState.IsValid) { if (pdc != null) { ViewBag.CreatePdcError = "Hãng sản xuất đã tồn tại."; } else if (createPdc.pdcName != null) { dbPdc.Producers.Add(createPdc); dbPdc.SaveChanges(); SetAlert("Thêm mới sản phẩm thành công!", "success"); return(RedirectToAction("Index", "Producer")); } else { ViewBag.CreatePdcError = "Tên hãng sản xuất không được bỏ trống."; } } } catch (Exception) { ViewBag.CreatePdcError = "Không thể thêm hãng sản xuất."; } } else { ViewBag.HinhAnh = "Vui lòng chọn hình ảnh."; } return(View()); } }
public ActionResult Create(Models.Product createPro, HttpPostedFileBase file) { //if (Session["accname"] == null) //{ // Session["accname"] = null; // return RedirectToAction("Login", "Account"); //} //else //{ ViewBag.pdcListCreate = new SelectList(dbPro.Producers, "pdcID", "pdcName"); ViewBag.typeListCreate = new SelectList(dbPro.ProductTypes, "typeID", "typeName"); var pro = dbPro.Products.SingleOrDefault(c => c.proID.Equals(createPro.proID)); if (file != null) { if (file.ContentLength > 0) { try { string nameFile = Path.GetFileName(file.FileName); file.SaveAs(Path.Combine(Server.MapPath("/Image"), nameFile)); createPro.proPhoto = "/Image/" + nameFile; } catch (Exception) { ViewBag.CreateProError = "Không thể chọn ảnh."; } } createPro.proUpdateDate = DateTime.Now.ToString(); try { if (pro != null) { ViewBag.CreateProError = "Mã sản phẩm đã tồn tại."; } else { dbPro.Products.Add(createPro); dbPro.SaveChanges(); //ViewBag.CreateProError = "Thêm sản phẩm thành công."; return(RedirectToAction("Index", new { erro = "Thêm sản phẩm thành công." })); } } catch (Exception) { ViewBag.CreateProError = "Không thể thêm sản phẩm."; } } else { ViewBag.HinhAnh = "Vui lòng chọn hình ảnh."; } return(View()); //} }
public ActionResult Create(Models.Category createCate, HttpPostedFileBase file) { if (Session["accname"] == null) { Session["accname"] = null; return(RedirectToAction("Login", "Account")); } else { if (file != null) { if (file.ContentLength > 0) { try { string nameFile = Path.GetFileName(file.FileName); file.SaveAs(Path.Combine(Server.MapPath("/Image"), nameFile)); createCate.catePhoto = "/Image/" + nameFile; } catch (Exception) { ViewBag.CreateCategory = "Không thể chọn ảnh."; } } try { if (dbCate.Categories.SingleOrDefault(cr => cr.cateName.Equals(createCate.cateName)) == null) { dbCate.Categories.Add(createCate); dbCate.SaveChanges(); ViewBag.CreateCategory = "Thêm danh mục thành công."; } else { ViewBag.CreateCategory = "Tên danh mục đã tồn tại."; } } catch (Exception) { ViewBag.CreateCategory = "Không thể thêm danh mục."; } } else { ViewBag.HinhAnh = "Vui lòng chọn hình ảnh."; } return(View()); } }
public ActionResult Create(Models.Administrator nd, string name, string mk, string diachi, string mail, string sdt, string hoten) { if (Session["accname"] == null) { Session["accname"] = null; return(RedirectToAction("Login", "Account")); } else { ViewBag.pdcListEdit = new SelectList(dbLog.Positions, "ID", "Name"); var pro = dbLog.Administrators.SingleOrDefault(c => c.adAcc.Equals(name)); try { if (pro != null) { ViewBag.CreatePdcError = "Tài khoản đã tồn tại"; return(View()); } else { nd.adAcc = name; nd.adPass = mk; nd.adHoten = hoten; nd.adEmail = mail; nd.adDiaChi = diachi; nd.adSDT = sdt; dbLog.Administrators.Add(nd); var mahoa = dao.Encrypt(nd.adPass); nd.adNgaytao = DateTime.Now; nd.adStatus = true; nd.adPass = mahoa; dbLog.SaveChanges(); SetAlert("Thêm mới danh mục thành công!", "success"); return(RedirectToAction("Index", "Account")); } } catch (Exception ex) { ViewBag.CreateProError = "."; return(View()); } } }
public ActionResult Create(Models.ProductType createType) { //if (Session["accname"] == null) //{ // Session["accname"] = null; // return RedirectToAction("Login", "Account"); //} //else //{ ViewBag.cateListCreate = new SelectList(dbType.Categories, "cateID", "cateName"); try { if (dbType.ProductTypes.SingleOrDefault(cr => cr.typeName.Equals(createType.typeName)) == null) { dbType.ProductTypes.Add(createType); dbType.SaveChanges(); //ViewBag.CreateTypeError = "Thêm loại sản phẩm thành công."; return(RedirectToAction("Index", new { error = "Thêm loại sản phẩm thành công." })); } else { ViewBag.CreateTypeError = "Tên danh mục đã tồn tại."; } //if (ModelState.IsValid) // { // dbType.ProductTypes.Add(createType); // dbType.SaveChanges(); // ViewBag.CreateTypeError = "Thêm loại sản phẩm thành công."; // } } catch (Exception) { ViewBag.CreateTypeError = "Không thể thêm loại sản phẩm."; } return(View()); //} }
public ActionResult Create(Models.News createNew) { if (Session["accname"] == null) { Session["accname"] = null; return(RedirectToAction("Login", "Account")); } else { ViewBag.pdcListCreate = new SelectList(news.Themes, "ID", "TenChuDe"); var pro = news.News.SingleOrDefault(c => c.TieuDe.Equals(createNew.TieuDe)); createNew.NgayDang = DateTime.Now; try { if (pro != null) { ViewBag.CreateProError = "Tên tiêu đề trùng."; return(View()); } else { news.News.Add(createNew); news.SaveChanges(); SetAlert("Thêm tin tức thành công", "success"); return(RedirectToAction("Index", "News")); } } catch (Exception) { ViewBag.CreateProError = "Không thể thêm sản phẩm."; } } return(View()); }
public ActionResult Create(Models.ProductType createType, string prtname) { if (Session["accname"] == null) { Session["accname"] = null; return(RedirectToAction("Login", "Account")); } else { ViewBag.cateListCreate = new SelectList(dbType.Categories, "cateID", "cateName"); try { EX ex = new EX(); var a = ex.prtname(prtname); int b = a.Count(); if (b == 0) { createType.typeName = prtname; dbType.ProductTypes.Add(createType); dbType.SaveChanges(); SetAlert("Thêm mới loại sản phẩm thành công!", "success"); return(RedirectToAction("Index", "ProductType")); } else { ViewBag.CreateTypeError = "Trùng tên loại sản phẩm."; } } catch (Exception) { ViewBag.CreateTypeError = "Không thể thêm loại sản phẩm."; } return(View()); } }
public ActionResult Create(Models.Category createCate, HttpPostedFileBase file, string name) { if (Session["accname"] == null) { Session["accname"] = null; return(RedirectToAction("Login", "Account")); } else { if (file != null) { if (file.ContentLength > 0) { try { string nameFile = Path.GetFileName(file.FileName); file.SaveAs(Path.Combine(Server.MapPath("/Image"), nameFile)); createCate.catePhoto = "/Image/" + nameFile; } catch (Exception) { ViewBag.CreateCategory = "Không thể chọn ảnh."; } } try { if (name != null) { EX ex = new EX(); var a = ex.prtname(name); int b = a.Count(); if (b == 0) { createCate.cateName = name; dbCate.Categories.Add(createCate); dbCate.SaveChanges(); SetAlert("Thêm mới danh mục thành công!", "success"); return(RedirectToAction("Index", "Category")); } else { ViewBag.CreateCategory = "Tên danh mục đã tồn tại."; } } else { ViewBag.CreateCategory = "Tên danh mục không được bỏ trống."; } } catch (Exception) { ViewBag.CreateCategory = "Không thể thêm danh mục."; } } else { ViewBag.HinhAnh = "Vui lòng chọn hình ảnh."; } return(View()); } }
public ActionResult Create(Models.Producer createPdc, HttpPostedFileBase file) { if (file != null) { if (file.ContentLength > 0) { try { //string nameFile = Path.GetFileName(file.FileName); string nameFile = Path.GetFileNameWithoutExtension(file.FileName); string extension = Path.GetExtension(file.FileName); nameFile = nameFile + extension; file.SaveAs(Path.Combine(Server.MapPath("/Image"), nameFile)); createPdc.pdcPhoto = "/Image/" + nameFile; } catch (Exception) { ViewBag.CreatePdcError = "Không thể chọn ảnh."; } } var pdc = dbPdc.Producers.SingleOrDefault(c => c.pdcName.Equals(createPdc.pdcName)); try { if (ModelState.IsValid) { if (pdc != null) { ViewBag.CreatePdcError = "Hãng sản xuất đã tồn tại."; } else { //ViewBag.CreatePdcError = "Thêm hãng sản xuất thành công."; dbPdc.Producers.Add(createPdc); dbPdc.SaveChanges(); return(RedirectToAction("Index", new { erro = "Thêm hãng sản xuất thành công." })); } } } catch (Exception) { ViewBag.CreatePdcError = "Không thể thêm hãng sản xuất."; } } else { ViewBag.HinhAnh = "Vui lòng chọn hình ảnh."; } return(View()); //if (Session["accname"] == null) //{ // Session["accname"] = null; // return RedirectToAction("Login", "Account"); //} //else //{ // if (file != null) // { // if (file.ContentLength > 0) // { // try // { // string nameFile = Path.GetFileName(file.FileName); // file.SaveAs(Path.Combine(Server.MapPath("/Image"), nameFile)); // createPdc.pdcPhoto = "/Image/" + nameFile; // } // catch (Exception) // { // ViewBag.CreatePdcError = "Không thể chọn ảnh."; // } // } // var pdc = dbPdc.Producers.SingleOrDefault(c => c.pdcName.Equals(createPdc.pdcName)); // try // { // if (ModelState.IsValid) // { // if (pdc != null) // { // ViewBag.CreatePdcError = "Hãng sản xuất đã tồn tại."; // } // else // { // dbPdc.Producers.Add(createPdc); // dbPdc.SaveChanges(); // ViewBag.CreatePdcError = "Thêm hãng sản xuất thành công."; // } // } // } // catch (Exception) // { // ViewBag.CreatePdcError = "Không thể thêm hãng sản xuất."; // } // } // else // { // ViewBag.HinhAnh = "Vui lòng chọn hình ảnh."; // } // return View(); //} }