public async Task <IActionResult> AddEditSelectItem(int id) { AddEditSelectItemViewModel model = new AddEditSelectItemViewModel(); model.SelectGroupListItems = await _context.SelectGroups.Select(c => new SelectListItem() { Text = c.Title, Value = c.Id.ToString() }).ToListAsync(); if (id != 0) { using (_serviceProvider.GetRequiredService <ApplicationDbContext>()) { SelectItem SelectItem = await _context.SelectItems.Where(a => a.Id == id).SingleOrDefaultAsync(); if (SelectItem != null) { model.Id = SelectItem.Id; model.Title = SelectItem.Title; model.IdSelectGroup = SelectItem.IdSelectGroup; } } } return(PartialView("AddEditSelectItem", model)); }
public async Task <IActionResult> AddEditSelectItem(int Id, AddEditSelectItemViewModel model, string redirectUrl) { if (ModelState.IsValid) { if (Id == 0) { using (var db = _serviceProvider.GetRequiredService <ApplicationDbContext>()) { SelectItem SelectItemModel = AutoMapper.Mapper.Map <AddEditSelectItemViewModel, SelectItem>(model); db.SelectItems.Add(SelectItemModel); await db.SaveChangesAsync(); } TempData["Notif"] = Notification.ShowNotif(MessageType.Add, type: ToastType.green); return(PartialView("_Succefullyresponse", redirectUrl)); } else { using (var db = _serviceProvider.GetRequiredService <ApplicationDbContext>()) { SelectItem SelectItemModel = AutoMapper.Mapper.Map <AddEditSelectItemViewModel, SelectItem>(model); db.SelectItems.Update(SelectItemModel); await db.SaveChangesAsync(); } TempData["Notif"] = Notification.ShowNotif(MessageType.Edit, type: ToastType.blue); return(PartialView("_Succefullyresponse", redirectUrl)); } } if (Id == 0) { TempData["Notif"] = Notification.ShowNotif(MessageType.addError, type: ToastType.yellow); } else { TempData["Notif"] = Notification.ShowNotif(MessageType.editError, type: ToastType.yellow); } model.SelectGroupListItems = await _context.SelectGroups.Select(c => new SelectListItem() { Text = c.Title, Value = c.Id.ToString() }).ToListAsync(); return(PartialView("AddEditSelectItem", model)); }