public async Task <ActionResult> Edit([Bind(Include = "Key,Code,Name,Plant")] DivisionEditViewModel division) { var div = await db.Divisions.Include(x => x.Plant).Where(x => x.Key == division.Key).SingleOrDefaultAsync(); if (div == null) { return(HttpNotFound()); } if (ModelState.IsValid) { if (div.Plant.Key != division.Plant) { div.Plant = await db.Plants.FindAsync(division.Plant); } if (div.Plant != null) { div.Code = division.Code; div.Name = division.Name; db.Entry(div).State = EntityState.Modified; await db.SaveChangesAsync(); return(RedirectToAction("Index")); } else { ModelState.AddModelError("Plant", "Selected plant is not found."); } } var Plants = await db.Plants.ToListAsync(); return(View(new DivisionEditViewModel(div, Plants.ToSelectList("Name", SelectedField: division.Plant)))); }
public async Task <IActionResult> Update([FromBody] DivisionEditViewModel input) { var user = User.GetDetails(); var data = _mapper.Map <DivisionEditViewModel, Division>(input); var division = await _unitOfWork.Divisions.GetDivision(input.Uid); division.Update(input.Name, input.Code, input.Status, user.Id); division.AddSubDivisions(data.SubDivisions.ToList(), user.Id); if (await _unitOfWork.Divisions.AnyAsync(division)) { return(BadRequest("Division Already Exists.")); } _unitOfWork.Divisions.Update(division); await _unitOfWork.CompleteAsync(user.Id); return(Ok(division)); }