public async Task <IActionResult> AddEditAgancy(int Id, AddEditAgencyViewModel model, string redirecturl) { if (ModelState.IsValid) { if (Id == 0) { using (var db = _iServiceProvider.GetRequiredService <ApplicationDbContext>()) { Agency agencymodel = AutoMapper.Mapper.Map <AddEditAgencyViewModel, Agency>(model); db.Agencies.Add(agencymodel); await db.SaveChangesAsync(); } TempData["Notif"] = Notification.ShowNotif(MessageType.Add, type: ToastType.green); return(PartialView("_Succefullyresponse", redirecturl)); } else { using (var db = _iServiceProvider.GetRequiredService <ApplicationDbContext>()) { Agency agencymodel = AutoMapper.Mapper.Map <AddEditAgencyViewModel, Agency>(model); db.Agencies.Update(agencymodel); 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.CitiesList = await _context.Cities.Select(c => new SelectListItem() { Text = c.Name, Value = c.Id.ToString() }).ToListAsync(); model.StatesList = await _context.States.Select(s => new SelectListItem() { Text = s.Name, Value = s.Id.ToString() }).ToListAsync(); return(PartialView("AddEditAgancy", model)); }
public async Task <IActionResult> AddEditAgancy(int Id) { AddEditAgencyViewModel model = new AddEditAgencyViewModel(); model.CitiesList = await _context.Cities.Select(c => new SelectListItem() { Text = c.Name, Value = c.Id.ToString() }).ToListAsync(); model.StatesList = await _context.States.Select(s => new SelectListItem() { Text = s.Name, Value = s.Id.ToString() }).ToListAsync(); if (Id != 0) { using (_iServiceProvider.GetRequiredService <ApplicationDbContext>()) { Agency agency = await _context.Agencies.Where(a => a.Id == Id).SingleOrDefaultAsync(); City city = await _context.Cities.Where(c => c.Id == agency.IdCity).SingleOrDefaultAsync(); if (agency != null) { model.Id = agency.Id; model.Title = agency.Title; model.FirstName = agency.FirstName; model.LastName = agency.LastName; model.IdState = city.StatesId; model.IdCity = agency.IdCity; model.Address = agency.Address; model.Plaque = agency.Plaque; model.PostalCode = agency.PostalCode; model.Tell = agency.Tell; } } } return(PartialView("AddEditAgancy", model)); }