public async Task <IActionResult> OnGetAsync() { if (Id.HasValue) { var dictionaryType = await dictionaryService.GetDictionaryTypeByIdAsync(Id.Value).ConfigureAwait(false); if (dictionaryType == null) { return(NotFound()); } ViewModel = new CreateOrUpdateDictionaryTypeDto { Id = dictionaryType.Id, Name = dictionaryType.Name, Remark = dictionaryType.Remark }; } else { ViewModel = new CreateOrUpdateDictionaryTypeDto(); } return(Page()); }
/// <summary> /// 创建或者更新字典类型 /// </summary> /// <param name="dto"></param> /// <returns></returns> public async Task <DataResult <long> > CreateOrUpdateDictionaryTypeAsync(CreateOrUpdateDictionaryTypeDto dto) { if (dto is null) { throw new ArgumentNullException(nameof(dto)); } if (!dto.Id.HasValue) { dto.Id = GuidEx.NewGuid(); DictionaryType entity = mapper.Map <DictionaryType>(dto); await applicationDbContext.DictionaryTypes.AddAsync(entity).ConfigureAwait(false); } else { var model = await applicationDbContext.DictionaryTypes.FirstOrDefaultAsync(e => e.Id == dto.Id).ConfigureAwait(false); model = mapper.Map(dto, model); } try { await applicationDbContext.SaveChangesAsync().ConfigureAwait(false); return(OkDataResult(dto.Id.Value)); } catch (DbUpdateException ex) { return(FailedDataResult <long>(ex)); } }
// To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see https://aka.ms/RazorPagesCRUD. public async Task <IActionResult> OnPostAsync(CreateOrUpdateDictionaryTypeDto ViewModel) { if (ViewModel is null) { throw new ArgumentNullException(nameof(ViewModel)); } if (!TryValidateModel(ViewModel)) { return(BadRequest(ModelState)); } var result = await dictionaryService.CreateOrUpdateDictionaryTypeAsync(ViewModel).ConfigureAwait(false); if (result.Succeeded) { return(new OkResult()); } ModelState.AddModelError(string.Empty, result.ErrorMessage); return(BadRequest(ModelState)); }