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 })); }
public ActionResult Create(ArtworkViewModel model, HttpPostedFileBase file) { var newID = 0; HttpPostedFileBase uploadfile = Request.Files["upload"]; 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); model.Photo = pic; string extension = System.IO.Path.GetExtension(model.Image.FileName); if (extension == ".png" || extension.Equals(".jpeg") || extension.Equals(".jpg")) { model.fileExtension = extension; } else { ModelState.AddModelError("", "Invalid File formats:Only png,jpeg,jpg are allowed"); throw new Exception("Invalid File formats:Only png,jpeg,jpg are allowed"); } } newID = model.Insert(model); if (newID > 0) { using (ArtworkViewModel ArtWorkViewMod = new ArtworkViewModel()) { viewModel = ArtWorkViewMod.GetObject(newID); return(RedirectToAction("Edit", new { id = newID })); } } } else { //model errors ModelState.AddModelError("", "There are form errors; please correct them to continue!"); } return(View(model)); }
public ActionResult Edit(Int32?id) { if (User.Identity.IsAuthenticated) { ArtworkViewModel ViewModel = new ArtworkViewModel(); if (id.HasValue) { ViewModel = ViewModel.GetObject(id.Value); } //does modle state have any errors return(View(ViewModel)); } else { return(RedirectToAction("Index", "ArtWorks")); } }