public ActionResult Edit(int id, UserViewModel userModel) { try { var user = users.GetList().FirstOrDefault(u => u.Id == id);// использую такой костыль т.к. в availableRewards возвращался неполный объект var userAwards = awards.GetList().Select(a => AwardViewModel.GetViewModel(a, user.GetAwards())); var checkedAwards = userModel.AvailableRewards.Where(a => a.Checked); var availableAwards = userAwards.Where(a => checkedAwards.Any(aw => aw.Id == a.Id)); var deletedAwards = userAwards.Where(a => !checkedAwards.Any(aw => aw.Id == a.Id)); foreach (var award in availableAwards) { award.Checked = true; users.AddAward(id, award.Id); } foreach (var award in deletedAwards) { award.Checked = false; users.RemoveAward(id, award.Id); } userModel.AvailableRewards = availableAwards.ToList(); users.Edit(userModel.ToUser()); return(RedirectToAction("Index")); } catch { return(View()); } }
private void btn_RewardUser_Click(object sender, EventArgs e) { var user = dgv_Users.CurrentRow?.DataBoundItem as User; if (user != null) { var form = new UserRewardForm(awardBL.GetList(), user.GetAwards().Select(a => a.Title)); if (form.ShowDialog() == DialogResult.OK) { var awardsNow = awardBL.GetList().Where(a => user.GetAwards().Any(ua => ua.Title == a.Title)); var selectedAwards = awardBL.GetList().Where(a => form.SelectedAwards.Contains(a.Title)); var awardToAdd = selectedAwards.Where(sa => !awardsNow.Any(a => a.Id == sa.Id)); var awardToDelete = awardsNow.Where(a => !selectedAwards.Any(sa => sa.Id == a.Id)); foreach (var toAdd in awardToAdd) { userBL.AddAward(user.Id, toAdd.Id); } foreach (var toDelete in awardToDelete) { userBL.RemoveAward(user.Id, toDelete.Id); } DisplayUsers(); } } }