public EditPlaylistView(PlaylistModel playlistModel) { InitializeComponent(); var vm = new EditPlayViewModel(playlistModel); this.DataContext = vm; vm.CloseAction += () => { CloseView(); }; }
public async Task <IActionResult> Edit(EditPlayViewModel model) { if (!ModelState.IsValid) { var actors = await _dataxContext.Actors.ToListAsync(); var actorsSelectList = new MultiSelectList(actors, "Id", "FirstName", model.Actors); ViewBag.Actors = actorsSelectList; return(View(model)); } var play = await _dataxContext.Plays.Include(p => p.PlayActors).FirstOrDefaultAsync(p => p.Id == model.Id); if (play == null) { return(NotFound()); } play.Title = model.Title; play.Description = model.Description; play.ScheduleTime = model.ScheduleTime; var playActors = new List <PlayActor>(); foreach (var id in model.Actors) { playActors.Add(new PlayActor() { PkFkActorId = id, PkFkPlayId = play.Id }); } if (model.NewImageFile != null) { using (var ms = new MemoryStream()) { await model.NewImageFile.CopyToAsync(ms); play.Image = null; play.Image = ms.ToArray(); } } _dataxContext.Plays.Update(play); _dataxContext.PlayActors.RemoveRange(play.PlayActors); _dataxContext.PlayActors.AddRange(playActors); await _dataxContext.SaveChangesAsync(); return(RedirectToAction("Index", "Home")); }
public ActionResult Edit(int id) { var playView = _playViewService.GetPlayView(id); var model = new EditPlayViewModel { Id = playView.Id, Title = playView.Title, Description = playView.Description, ScheduledTime = playView.ScheduledTime, ActorsString = playView.Actors, ImageVirtualPath = playView.ImageVirtualPath, ImagePath = playView.ImagePath, ImageType = playView.ImageType, AllActors = _actorService.GetAllActors() }; return(View(model)); }
public ActionResult Edit(EditPlayViewModel model) { if (ModelState.IsValid) { var currentStatePlayView = _playViewService.GetPlayView(model.Id); if (model.ScheduledTime == null) { model.ScheduledTime = currentStatePlayView.ScheduledTime; } if (model.SelectedActorsIds.Count == 0) { var playActorsList = _playActorService.GetPlayActorsForPlayId(model.Id); foreach (var playActorRecord in playActorsList) { model.SelectedActorsIds.Add(playActorRecord.ActorId); } } if (model.File != null) { if (model.File.ContentLength > 0 && !model.File.ContentType.Contains("image")) { ModelState.AddModelError(nameof(PlayViewModel.File), "File must be image type!"); model.AllActors = _actorService.GetAllActors(); return(View(model)); } var imageFolderPath = Server.MapPath(WebConfigurationManager.AppSettings["imagePath"].ToString()); var fileName = Guid.NewGuid().ToString(); model.ImageVirtualPath = string.Format("{0}{1}{2}", WebConfigurationManager.AppSettings["virtualImagePath"].ToString(), fileName, Path.GetExtension(model.File.FileName).ToLower()); model.ImagePath = string.Format("{0}{1}{2}", imageFolderPath, Path.GetFileName(fileName), Path.GetExtension(model.File.FileName).ToLower()); model.File.SaveAs(model.ImagePath); PlayWithActors playWithActors = new PlayWithActors { Title = model.Title, ImagePath = model.ImagePath, ImageVirtualPath = model.ImageVirtualPath, ImageType = Path.GetExtension(model.File.FileName), Description = model.Description, ScheduledTime = model.ScheduledTime, ActorsIds = model.SelectedActorsIds }; _playService.UpdatePlay(playWithActors); return(RedirectToAction("Index")); } else if (model.File == null) { PlayWithActors playWithActors = new PlayWithActors { Id = model.Id, Title = model.Title, ImagePath = currentStatePlayView.ImagePath, ImageVirtualPath = currentStatePlayView.ImageVirtualPath, ImageType = currentStatePlayView.ImageType, Description = model.Description, ScheduledTime = model.ScheduledTime, ActorsIds = model.SelectedActorsIds }; _playService.UpdatePlay(playWithActors); return(RedirectToAction("Index")); } } model.ActorsString = _playViewService.GetPlayView(model.Id).Actors; model.AllActors = _actorService.GetAllActors(); return(View(model)); }