public ActionResult Create(int? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } var model = new CreatePhotoViewModel {GalleryId = (int) id}; return View(model); }
public ActionResult Create(CreatePhotoViewModel model) { if (model.PhotoUpload == null || model.PhotoUpload.ContentLength == 0) { ModelState.AddModelError("PhotoUpload", "This field is required"); } if (model.PhotoUpload.ContentLength >= 5000000) { ModelState.AddModelError("PhotoUpload", "Photo is too big! Please resize to under 5mb or contact Alex"); } if (ModelState.IsValid) { PhotoUploader photoUploader = new PhotoUploader(); var photo = new Photo { Title = model.Title, Description = model.Description, GalleryId = model.GalleryId, FileName = model.PhotoUpload.FileName, Width = Image.FromStream(model.PhotoUpload.InputStream, false, false).Width, Height = Image.FromStream(model.PhotoUpload.InputStream, false, false).Height }; if (model.PhotoUpload != null && model.PhotoUpload.ContentLength > 0) { photo = photoUploader.SavePhotoToFileSystem(photo, model.PhotoUpload); repository.Add(photo); repository.Save(); } return RedirectToAction("ViewGallery", "Gallery", new {id = model.GalleryId}); } return View(model); }