public ActionResult Create(Product product, HttpPostedFileBase fileupload) { if (ModelState.IsValid) { #region Upload and resize image if needed if (fileupload != null) { string filename = Path.GetFileName(fileupload.FileName); string newFilename = Guid.NewGuid().ToString().Replace("-", string.Empty) + Path.GetExtension(filename); string newFilenameUrl = "/Uploads/product/" + newFilename; string physicalFilename = Server.MapPath(newFilenameUrl); fileupload.SaveAs(physicalFilename); product.ImageUrl = newFilenameUrl; } #endregion product.LikeNumber = 0; product.Code = CodeCreator.ReturnProductCode(); product.IsDeleted = false; product.CreationDate = DateTime.Now; product.Id = Guid.NewGuid(); db.Products.Add(product); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.ArtistId = new SelectList(db.Artists, "Id", "FullName", product.ArtistId); ViewBag.ProductGroupId = new SelectList(db.ProductGroups, "Id", "Title", product.ProductGroupId); return(View(product)); }
public ActionResult Create(Product product, HttpPostedFileBase fileupload, HttpPostedFileBase headerUpload) { if (ModelState.IsValid) { #region Upload and resize image if needed if (fileupload != null) { string filename = Path.GetFileName(fileupload.FileName); string newFilename = Guid.NewGuid().ToString().Replace("-", string.Empty) + Path.GetExtension(filename); string newFilenameUrl = "/Uploads/product/" + newFilename; string physicalFilename = Server.MapPath(newFilenameUrl); fileupload.SaveAs(physicalFilename); product.ImageUrl = newFilenameUrl; } if (headerUpload != null) { string filename = Path.GetFileName(headerUpload.FileName); string newFilename = Guid.NewGuid().ToString().Replace("-", string.Empty) + Path.GetExtension(filename); string newFilenameUrl = "/Uploads/product/" + newFilename; string physicalFilename = Server.MapPath(newFilenameUrl); headerUpload.SaveAs(physicalFilename); product.HeaderUrl = newFilenameUrl; } #endregion product.Code = CodeCreator.ReturnProductCode(); product.IsDeleted = false; product.CreationDate = DateTime.Now; product.Id = Guid.NewGuid(); db.Products.Add(product); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.ProductGroupId = new SelectList(db.ProductGroups.Where(current => current.IsDeleted == false), "Id", "Title", product.ProductGroupId); ViewBag.ProductMediumId = new SelectList(db.ProductMediums.Where(current => current.IsDeleted == false), "Id", "Title", product.ProductMediumId); ViewBag.ProductOrientationId = new SelectList(db.ProductOrientations.Where(current => current.IsDeleted == false), "Id", "Title", product.ProductOrientationId); ViewBag.ProductThemeId = new SelectList(db.ProductThemes.Where(current => current.IsDeleted == false), "Id", "Title", product.ProductThemeId); ViewBag.ProductTypeId = new SelectList(db.ProductTypes.Where(current => current.IsDeleted == false), "Id", "Title", product.ProductTypeId); ViewBag.ProductColorId = new SelectList(db.ProductColors.Where(current => current.IsDeleted == false), "Id", "Title", product.ProductColorId); ViewBag.SellerId = new SelectList(db.Sellers.Where(current => current.IsDeleted == false), "Id", "Title", product.SellerId); return(View(product)); }