public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } _context.Category.Add(Category); await _context.SaveChangesAsync(); int CategoryId = Category.CategoryId; parentParamterIds = Request.Form["AddParentParameters"]; string[] parentParamterIdsArrayString = parentParamterIds.Split(","); int[] parentParamterIdsArrayInt = new int[parentParamterIdsArrayString.Length]; for (int i = 0; i < parentParamterIdsArrayString.Length; i++) { parentParamterIdsArrayInt[i] = Convert.ToInt32(parentParamterIdsArrayString[i].ToString()); } foreach (var ppId in parentParamterIdsArrayInt) { CategoryParentParameter = new Data.CategoryParentParameter() { CategoryId = CategoryId, ParentParameterId = ppId }; _context.CategoryParentParameter.Add(CategoryParentParameter); await _context.SaveChangesAsync(); } var user = await _userManager.GetUserAsync(User); logger.Info("{user} created category {category} - carrying id {id} on {date}", user.Email, Category.CategoryName, Category.CategoryId, DateTime.Now); return(RedirectToPage("../Categories")); }
public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } var ppRemoveIds = _context.CategoryParentParameter.Where(c => EF.Property <int>(c, "CategoryId") == Category.CategoryId); foreach (var ppRemoveIdsFE in ppRemoveIds) { _context.CategoryParentParameter.Remove(ppRemoveIdsFE); } _context.Attach(Category).State = EntityState.Modified; int CategoryId = Category.CategoryId; parentParamterIds = Request.Form["AddParentParameters"]; if (parentParamterIds != null) { string[] parentParamterIdsArrayString = parentParamterIds.Split(","); int[] parentParamterIdsArrayInt = new int[parentParamterIdsArrayString.Length]; for (int i = 0; i < parentParamterIdsArrayString.Length; i++) { parentParamterIdsArrayInt[i] = Convert.ToInt32(parentParamterIdsArrayString[i].ToString()); } foreach (var ppId in parentParamterIdsArrayInt) { CategoryParentParameter = new Data.CategoryParentParameter() { CategoryId = CategoryId, ParentParameterId = ppId }; _context.CategoryParentParameter.Add(CategoryParentParameter); await _context.SaveChangesAsync(); } } try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CategoryExists(Category.CategoryId)) { return(NotFound()); } else { throw; } } var user = await _userManager.GetUserAsync(User); logger.Info("{user} edited category {category} - carrying {id} on {date}", user.Email, Category.CategoryName, Category.CategoryId, DateTime.Now); return(RedirectToPage("../Categories")); }