public ActionResult SaveServiceOption(ServiceOptionAbbreviatedModel option, ICollection <string> userInputs = null, HttpPostedFileBase image = null) { /*there is way too much code in this controller */ var existingOption = _portfolioService.GetServiceOption(UserId, option.Id); //option to amend if (image != null) { if (existingOption.Picture != null) /* deal with previous picture by deleting it */ { var path = Path.Combine(ConfigHelper.GetOptionPictureLocation(), option.Picture.ToString()); try //catch error if key is not in web.config { System.IO.File.Delete(Server.MapPath(path)); } catch (Exception exception) { TempData["MessageType"] = WebMessageType.Failure; //unable to delete, exit at this point TempData["Message"] = $"Failed to delete existing file, error: {exception.Message}"; return(RedirectToAction("UpdateServiceOption", new { id = option.Id })); } } option.PictureMimeType = image.ContentType; //rename file to a guid and store original file type option.Picture = Guid.NewGuid(); try { var path = Path.Combine(ConfigurationManager.AppSettings["OptionPicsPath"], option.Picture.ToString()); //save file image.SaveAs(Server.MapPath(path)); } catch (Exception exception) { TempData["MessageType"] = WebMessageType.Failure; TempData["Message"] = $"Failed to save file, error: {exception.Message}"; return(RedirectToAction("UpdateServiceOption", new { id = option.Id })); } } else //preserve picture data for now { if (option.Id > 0) { var tempOption = _portfolioService.GetServiceOption(UserId, option.Id); option.Picture = tempOption.Picture; option.PictureMimeType = tempOption.PictureMimeType; } } /*end of dealing with pictures */ /* deal with user inputs */ _portfolioService.RemoveInputsFromServiceOption(UserId, option.Id, _portfolioService.GetInputsForServiceOptions(UserId, new[] { new ServiceOptionDto { Id = option.Id } })); if (userInputs != null) { var inputGroup = UserInputHelper.MakeInputGroup(userInputs); _portfolioService.AddInputsToServiceOption(UserId, option.Id, inputGroup); } try { existingOption = AbbreviatedEntityUpdate.UpdateServiceOption(option, existingOption); _portfolioService.ModifyServiceOption(UserId, existingOption, EntityModification.Update); } catch (Exception exception) { TempData["MessageType"] = WebMessageType.Failure; TempData["Message"] = $"Failed to save option, error: {exception.Message}"; return(RedirectToAction("ShowServiceOption", new { id = option.Id })); } return(RedirectToAction("ShowServiceOption", new { id = option.Id })); }