public ActionResult Edit(ExampleParentViewModel model) { if (ModelState.IsValid) { var parent = _exampleParentsRepository.Get(model.Id); parent.Text = model.Text; parent.LongText = model.LongText; if (model.RemovePhoto) { _imagesRepository.Remove(parent.PhotoId.Value); parent.PhotoId = null; _exampleParentsRepository.Update(parent); _exampleParentsRepository.SaveChanges(); _imagesRepository.SaveChanges(); } else if (model.Upload != null) { try { var image = new Photo() { ObjectId = parent.Id, ObjectTypeName = nameof(ExampleParent), FileName = $"Image{parent.Id}", DirectoryPath = "ExampleParents", Title = "NoTitle" }; _imagesRepository.Upload(image, model.Upload); _imagesRepository.SaveChanges(); parent.PhotoId = image.Id; } catch (Exception ex) { ModelState.AddModelError(string.Empty, ex.Message); } } _exampleParentsRepository.Update(parent); _exampleParentsRepository.SaveChanges(); return(RedirectToAction(JMap.Example.ExampleParents.List())); } return(View(model)); }
public ActionResult Create(ExampleParentViewModel model) { if (ModelState.IsValid) { var parent = new ExampleParent() { Text = model.Text, LongText = model.LongText }; _exampleParentsRepository.Create(parent); _exampleParentsRepository.SaveChanges(); if (model.Upload != null) { try { var photo = new Photo() { ObjectId = parent.Id, ObjectTypeName = nameof(ExampleParent), FileName = $"Image{parent.Id}", DirectoryPath = "ExampleParents", Title = "NoTitle" }; _imagesRepository.Upload(photo, model.Upload); _imagesRepository.SaveChanges(); parent.PhotoId = photo.Id; _exampleParentsRepository.Update(parent); _exampleParentsRepository.SaveChanges(); } catch (Exception ex) { ModelState.AddModelError(string.Empty, ex.Message); } } return(RedirectToAction(JMap.Example.ExampleParents.List())); } return(View(model)); }
public ActionResult Create() { var model = new ExampleParentViewModel(); return(View(model)); }