public async Task <IActionResult> Edit(int id, EditProjectViewModel model) { if (!ModelState.IsValid) { model.Items = PopulateProjectByEdit(model.Id); return(View(model)); } if (IsProjectNameExitsByEdit(model.ProjectName, model.Id) == false) { ModelState.AddModelError(string.Empty, "项目名称已经存在"); } if (ModelState.ErrorCount > 0) { model.Items = PopulateProjectByEdit(model.Id); return(View(model)); } var dbProject = _hdDbContext.Projects.FirstOrDefault(t => t.Id == model.Id); var dbGroup = _hdDbContext.Groups.FirstOrDefault(t => t.Id == int.Parse(model.GroupId)); dbProject.Name = model.ProjectName; dbProject.GroupId = dbGroup.Id; dbProject.Group = dbGroup; _hdDbContext.Update(dbProject); await _hdDbContext.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); }
public async Task <IActionResult> Edit(EditGroupViewModel editGroupViewModel) { if (!ModelState.IsValid) { editGroupViewModel.Items = PopulateIndustriesByEdit(editGroupViewModel.Id); return(View(editGroupViewModel)); } if (editGroupViewModel.IndustryId == "-1") { ModelState.AddModelError(string.Empty, "必须选择行业"); } if (IsGroupNameExistsByEdit(editGroupViewModel.GroupName, editGroupViewModel.Id) == false) { ModelState.AddModelError(string.Empty, "集团名称已经存在"); } if (ModelState.ErrorCount > 0) { editGroupViewModel.Items = PopulateIndustriesByEdit(editGroupViewModel.Id); return(View(editGroupViewModel)); } var dbGroup = _hdDbContext.Groups.FirstOrDefault(t => t.Id == editGroupViewModel.Id); var dbIndustry = _hdDbContext.Industries.FirstOrDefault(t => t.Id == int.Parse(editGroupViewModel.IndustryId)); dbGroup.Name = editGroupViewModel.GroupName; dbGroup.IndustryId = dbIndustry.Id; dbGroup.Industry = dbIndustry; _hdDbContext.Update(dbGroup); await _hdDbContext.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); }