public ActionResult Edit(ArtworkViewModel model) { var id = 0; var viewModel = new ArtworkViewModel(); if (ModelState.IsValid) { if (model.Image != null) { //image cannot be length cannot be more than byte[] pic = this.ConvertToBytes(model.Image); string extension = System.IO.Path.GetExtension(model.Image.FileName); if (extension == ".png" || extension.Equals(".jpeg") || extension.Equals(".jpg")) { model.fileExtension = extension; model.Photo = pic; id = model.Update(model); if (id > 0) { using (ArtworkViewModel ArtWorkViewMod = new ArtworkViewModel()) { viewModel = ArtWorkViewMod.GetObject(model.ArtWorkID); } } } else { ModelState.AddModelError("Image", "Invalid File formats:Only png,jpeg,jpg are allowed"); //throw new Exception("Invalid File formats:Only png,jpeg,jpg are allowed"); } } else { id = model.Update(model); if (id > 0) { using (ArtworkViewModel ArtWorkViewMod = new ArtworkViewModel()) { viewModel = ArtWorkViewMod.GetObject(model.ArtWorkID); } } } //map } else { //model errors ModelState.AddModelError("", "There are form errors; please correct them to continue!"); } // return RedirectToAction("Edit", new { id = model.ArtWorkID }); return(View(model)); }
public ActionResult Save(ArtworkViewModel model, HttpPostedFileBase file) { var id = 0; var viewModel = new ArtworkViewModel(); if (ModelState.IsValid) { if (model.Image != null) { byte[] pic = this.ConvertToBytes(model.Image); string extension = System.IO.Path.GetExtension(model.Image.FileName); if (extension == ".png" || extension.Equals(".jpeg") || extension.Equals(".jpg")) { model.fileExtension = extension; viewModel = model; viewModel.Photo = pic; } else { throw new Exception("Invalid File formats:Only png,jpeg,jpg are allowed"); } } id = model.Update(viewModel); if (id > 0) { using (ArtworkViewModel ArtWorkViewMod = new ArtworkViewModel()) { viewModel = ArtWorkViewMod.GetObject(model.ArtWorkID); } } } else { //model errors ModelState.AddModelError("", "There are form errors; please correct them to continue!"); } return(RedirectToAction("Edit", new { id = model.ArtWorkID })); }