public ActionResult Add(AddLaptopBm bind, IEnumerable <HttpPostedFileBase> images) { if (this.ModelState.IsValid) { foreach (var img in images) { if (img != null) { if (img.ContentLength > (5 * 1024 * 1024)) { ModelState.AddModelError("CustomError", "File size must be less than 5 MB"); return(View()); } if (img.ContentType != "image/jpeg") { ModelState.AddModelError("CustomError", "File type must be \"jpeg\""); return(View()); } } } this.service.AddNewLaptop(bind, images); return(RedirectToAction("AdminLaptopsList")); } return(this.View()); }
public void AddNewLaptop(AddLaptopBm bind, IEnumerable <HttpPostedFileBase> images) { Laptop laptop = Mapper.Map <AddLaptopBm, Laptop>(bind); int i = 1; foreach (var img in images) { if (img != null) { switch (i) { case 1: laptop.Image1 = new byte[img.ContentLength]; img.InputStream.Read(laptop.Image1, 0, img.ContentLength); break; case 2: laptop.Image2 = new byte[img.ContentLength]; img.InputStream.Read(laptop.Image2, 0, img.ContentLength); break; case 3: laptop.Image3 = new byte[img.ContentLength]; img.InputStream.Read(laptop.Image3, 0, img.ContentLength); break; case 4: laptop.Image4 = new byte[img.ContentLength]; img.InputStream.Read(laptop.Image4, 0, img.ContentLength); break; case 5: laptop.Image5 = new byte[img.ContentLength]; img.InputStream.Read(laptop.Image5, 0, img.ContentLength); break; } i++; } } this.Context.Laptops.Add(laptop); this.Context.SaveChanges(); }