private async Task <WorkerSetting> GetWorkerSetting(ApplicationUser userWorker) { WorkerSetting workerSetting; try { workerSetting = userWorker.Worker.WorkerSettingJson.ParseToWorkerSetting(); } catch { workerSetting = new WorkerSetting { //CityId = userWorker.CityId, //CategoryIds = await Context.Categories.Where(c => c.ParentId != null).Select(c => c.Id).ToListAsync(), //DistrictIds = await Context.Districts.Where(d => d.CityId == userWorker.CityId).Select(d => d.Id).ToListAsync() CityId = userWorker.CityId, CategoryIds = new List <int>(), DistrictIds = new List <int>() }; userWorker.Worker.WorkerSettingJson = workerSetting.ToJSON(); Context.Update(userWorker.Worker); await Context.SaveChangesAsync(); } return(workerSetting); }
public async Task <IActionResult> SaveUser(UserPageViewModel model) { if (!ModelState.IsValid) { model.Cities = await _context.Cities.ToListAsync(); model.City = model.Cities.Where(c => c.Id == model.CityId).Select(c => c.Name).SingleOrDefault(); return(View(model)); } UserInformer informer = new UserInformer(User, _context, _userManager); var user = await informer.GetUserWorkerAsync(); if (user != null) { user.UserName = model.Name; user.PhoneNumber = model.PhoneNumber; user.CityId = model.CityId; WorkerSetting workerSettings = user.Worker.WorkerSettingJson.ParseToWorkerSetting(); if (workerSettings.CityId != model.CityId) { workerSettings.CityId = model.CityId; workerSettings.DistrictIds = new List <int>(); user.Worker.WorkerSettingJson = workerSettings.ToJSON(); } _context.Entry(user).State = EntityState.Modified; _context.Entry(user.Worker).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { } } return(RedirectToAction(nameof(Index))); }