//[Route("Create")] //add Obj into DB //[HttpPost] public IActionResult Create(ProductCategoryModel model) { //Have value if (ModelState.IsValid) //required name and alias ? { ProductCategoryModel obj = new ProductCategoryModel(); obj.Name = model.Name; obj.Alias = model.Alias; obj.Code = model.Code; obj.Description = model.Description; obj.ParentID = model.ParentID; obj.DisplayOrder = model.DisplayOrder; obj.Status = model.Status; obj.HomeFlag = model.HomeFlag; //var dt = DateTime.Now.ToString("dd/MM/yyyy").Replace('-', '/'); //obj.CreatedDate = Convert.ToDateTime(dt); obj.CreatedDate = DateTime.Now; TempData["Success"] = "Thêm mới thành công"; //add DB _webBHDbContext.Add(obj); int result = _webBHDbContext.SaveChanges(); int Id = _webBHDbContext.ProductCategories.Max(x => x.ID); if (result > 0) { //view thông tin PC ViewBag.PC = model; //dm cha ViewBag.ListPC = _webBHDbContext.ProductCategories.ToList(); //return View("Edit", Id); //return RedirectToAction("Edit", "ProductCategory", new { Id }); // return Redirect("/Admin/ProductCategory/Index"); return(RedirectToAction(nameof(Index))); } else { TempData["addFail"] = "Thêm mới thất bại"; return(RedirectToAction("Edit", "ProductCategory", new { Id })); } } //else if (!ModelState.IsValid) // { // //error // var errors = ModelState.Values.SelectMany(v => v.Errors); // } /* action*/ return(RedirectToAction("Index")); }
//[HttpPost] public IActionResult Create(SliderModel model, IFormFile Image) { if (ModelState.IsValid) { SliderModel obj = new SliderModel(); obj.Name = model.Name; obj.DisplayOrder = model.DisplayOrder; obj.HomeFlag = model.HomeFlag; obj.Content = model.Content; //date obj.CreatedDate = DateTime.Now; //image if (Image != null || Image.Length > 0) { var extension = ""; //get đuôi file extension = System.IO.Path.GetExtension(Image.FileName); var unique = DateTime.Now.ToString("yyMMdd_HHmmss"); var path = Path.Combine(_environment.WebRootPath, "Uploads/ImageSlider") + $@"\{Image.FileName}" + "_" + unique + extension; /*Directory.GetCurrentDirectory(), "wwwroot/Uploads/", Imageme.FileName);*/ var stream = new FileStream(path, FileMode.Create); Image.CopyToAsync(stream); //Save database //image obj.Image = Image.FileName + "_" + unique + extension; } TempData["Success"] = "Thêm mới thành công"; _webBHDbContext.Add(obj); int result = _webBHDbContext.SaveChanges(); var id = _webBHDbContext.Sliders.OrderByDescending(x => x.ID).FirstOrDefault(); if (result > 0) { return(RedirectToAction("Edit", "Slider", new { id.ID })); } } return(RedirectToAction("AddSliderForm")); }
//Add public IActionResult Create(ProductModel model, IFormFile Imageme, List <IFormFile> MoreImage) { if (ModelState.IsValid) { ProductModel obj = new ProductModel(); obj.Name = model.Name; obj.Alias = model.Alias; obj.CategoryID = model.CategoryID; //image if (Imageme != null || Imageme.Length > 0) { var extension = ""; //get đuôi file extension = System.IO.Path.GetExtension(Imageme.FileName); var unique = DateTime.Now.ToString("yyMMdd_HHmmss"); var path = Path.Combine(_environment.WebRootPath, "Uploads/ImageProduct") + $@"\{Imageme.FileName}" + "_" + unique + extension; /*Directory.GetCurrentDirectory(), "wwwroot/Uploads/", Imageme.FileName);*/ var stream = new FileStream(path, FileMode.Create); Imageme.CopyToAsync(stream); //Save database //image obj.Imageme = Imageme.FileName + "_" + unique + extension; } //more image foreach (var item in MoreImage) { if (item.Length > 0) { //đuôi file var extension = ""; extension = System.IO.Path.GetExtension(item.FileName); //không trùng file var unique = DateTime.Now.ToString("mmss"); var path = Path.Combine(_environment.WebRootPath, "Uploads/ImageProduct") + $@"\{item.FileName}" + unique + extension; /*Directory.GetCurrentDirectory(), "wwwroot/Uploads/", Imageme.FileName);*/ var stream = new FileStream(path, FileMode.Create); item.CopyToAsync(stream); //image obj.MoreImage += item.FileName + unique + extension + "_"; } } obj.Code = model.Code; obj.Price = model.Price; obj.PromotionPrice = model.PromotionPrice; obj.Warranty = model.Warranty; obj.Quantity = model.Quantity; obj.Description = model.Description; obj.Content = model.Content; //condition obj.HomeFlag = model.HomeFlag; obj.HotFlag = model.HotFlag; obj.Status = model.Status; obj.TopSales = model.TopSales; obj.CreatedDate = DateTime.Now; TempData["Success"] = "Thêm mới thành công"; _webBHDbContext.Add(obj); int result = _webBHDbContext.SaveChanges(); //int intIdt = db.Users.Max(u => u.UserId); var id = _webBHDbContext.Products.OrderByDescending(x => x.ID).FirstOrDefault(); if (result > 0) { //ViewBag.P = model; //ViewBag.ListP = _webBHDbContext.ProductCategories.ToList(); ////GET Image //ViewBag.Image = obj.Imageme; //if (obj.MoreImage != null) //{ // ViewBag.MoreImage = obj.MoreImage.Split("_"); //} return(RedirectToAction("Edit", "Product", new { id.ID })); } else { TempData["addFail"] = "Thêm mới thất bại"; return(RedirectToAction("Index")); } } //else if (!ModelState.IsValid) //{ // //error // var errors = ModelState.Values.SelectMany(v => v.Errors); //} return(RedirectToAction("AddProductForm")); }