public ActionResult Create(Product product, string Category, HttpPostedFileBase ImageFile) { if (ImageFile == null) { mImage mImage = new mImage() { Path = $"~/Images/default.jpg", Name = "default" }; product.Image = mImage; } else { product.Image = AddImage(ImageFile); } if (ModelState.IsValid) { Category cat = DbContext.Category.Find(Int32.Parse(Category)); product.Category = cat; DbContext.Product.Add(product); DbContext.SaveChanges(); return(RedirectToAction("Index")); } return(View(product)); }
public mImage AddImage(HttpPostedFileBase ImageFile) { string extension = Path.GetExtension(ImageFile.FileName); string fileName = $"{Path.GetFileNameWithoutExtension(ImageFile.FileName)}{DateTime.Now.ToString("yymmssfff")}{extension}"; string FileName = Path.Combine(Server.MapPath("~/Images/"), fileName); mImage mImage = new mImage() { Path = $"~/Images/{fileName}", Name = Path.GetFileNameWithoutExtension(ImageFile.FileName) }; ImageFile.SaveAs(FileName); string result = string.Concat("a", "b"); return(mImage); }