public ActionResult Edit(AlbumViewModel model, HttpPostedFileBase image) { if (ModelState.IsValid) { bool newAlbum = false; if (db.Albums.Find(model.Id) == null) { newAlbum = true; } Album album = ApplyItemChanges(model); if (image != null) { AddImageToAlbum(album, image); } if (newAlbum) { db.Albums.Add(album); } else { db.Entry(album).State = EntityState.Modified; } db.SaveChanges(); return RedirectToAction("Index"); } return View(model); }
private AlbumViewModel EditViewModelInitializer() { AlbumViewModel result = new AlbumViewModel { list = new List<SelectListItem>() }; foreach (var item in GetAutorsList()) { result.list.Add(new SelectListItem { Text = item.Name }); } return result; }
private Album ApplyItemChanges(AlbumViewModel model) { Album result = GetAlbum(model.Id); if (result == null) { result=new Album(); } result.Name = model.Name; result.Description = model.Description; result.Rate = model.Rate; result.AutorId = db.Autors.FirstOrDefault(a => a.Name == model.AutorName).AutorId; return result; }
private AlbumViewModel EditViewModelInitializer(Album album) { AlbumViewModel result = new AlbumViewModel { Description = album.Description, Id = album.AlbumId, list = new List<SelectListItem>(), Name = album.Name, AutorName = GetAutor(album.AutorId).Name, Rate = album.Rate }; foreach (var item in GetAutorsList()) { result.list.Add(new SelectListItem {Text = item.Name}); } return result; }