public async Task <IActionResult> Edit(CourtCasesViewModel model) { if (ModelState.IsValid) { await this.courtCasesService.EditAsync(model); return(RedirectToAction(nameof(Index))); } model.CourtTypes = this.courtCasesService.GetAllCourtTypes(); model.LawCases = this.courtCasesService.GetAllLawCases(); return(View(model)); }
public async Task CourtCasesEditTest() { var optionBuilder = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase("testDb"); var dbContext = new ApplicationDbContext(optionBuilder.Options); var service = new CourtCaseService(dbContext); var model = new CourtCasesInputModel { CaseNumber = 123, CaseYear = 2020, CaseType = "Исково", CourtChamber = "районен", }; var model2 = new CourtCasesInputModel { CaseNumber = 124, CaseYear = 2020, CaseType = "Исково", CourtChamber = "районен", }; await service.CreateAsync(model); await service.CreateAsync(model2); var myId1 = await dbContext.CourtCases.Where(x => x.Id == 1).Select(x => x.Id).FirstOrDefaultAsync(); var myId2 = await dbContext.CourtCases.Where(x => x.Id == 2).Select(x => x.Id).FirstOrDefaultAsync(); var editModel = new CourtCasesViewModel { CaseNumber = 111, CaseYear = 2020, CaseType = "Исково", CourtChamber = "районен", Id = 1, }; var result = service.EditAsync(editModel); var myCourtCase = dbContext.CourtCases.Where(x => x.Id == 1).FirstOrDefault(); Assert.NotNull(result); //Assert.True(result.IsCompletedSuccessfully); //Assert.Equal(1, myCourtCase.Id); //Assert.Equal("Исково", myCourtCase.CaseType.ToString()); }
//public async Task<CourtCasesViewModel> EditAsync(int? id) //{ // return await this.dbContext.CourtCases.Where(x => x.Id == id).Select(c => new CourtCasesViewModel // { // Id = c.Id, // CaseNumber = c.CaseNumber, // CaseYear = c.CaseYear, // CaseType = c.CaseType.ToString(), // CourtChamber = c.CourtChamber, // CourtId = c.CourtId, // LawCaseId = c.LawCaseId, // }).FirstOrDefaultAsync(); //} public async Task EditAsync(CourtCasesViewModel model) { var courtCase = new CourtCase { Id = model.Id, CaseNumber = model.CaseNumber, CaseYear = model.CaseYear, CaseType = Enum.Parse <CaseType>(model.CaseType, true), CourtChamber = model.CourtChamber, CourtId = model.CourtId, LawCaseId = model.LawCaseId, }; this.dbContext.CourtCases.Update(courtCase); await this.dbContext.SaveChangesAsync(); }