public async Task<ActionResult> CustomFieldUpdate(MetaField metaField, List<int> Categories) { if (metaField.ID == 0) { metaField.ObjectState = Repository.Pattern.Infrastructure.ObjectState.Added; _customFieldService.Insert(metaField); } else { var metaFieldExistingQuery = await _customFieldService.Query(x => x.ID == metaField.ID).Include(x => x.MetaCategories).SelectAsync(); var metaFieldExisting = metaFieldExistingQuery.FirstOrDefault(); metaFieldExisting.Name = metaField.Name; metaFieldExisting.ControlTypeID = metaField.ControlTypeID; metaFieldExisting.Options = metaField.Options; metaFieldExisting.Required = metaField.Required; metaFieldExisting.Searchable = metaField.Searchable; metaFieldExisting.ObjectState = Repository.Pattern.Infrastructure.ObjectState.Modified; _customFieldService.Update(metaFieldExisting); // Delete existing foreach (var category in metaFieldExisting.MetaCategories) { await _customFieldCategoryService.DeleteAsync(category.ID); } } if (Categories != null) { // Insert meta categories var metaCategories = Categories.Select(x => new MetaCategory() { CategoryID = x, FieldID = metaField.ID, ObjectState = Repository.Pattern.Infrastructure.ObjectState.Added }).ToList(); _customFieldCategoryService.InsertRange(metaCategories); } await _unitOfWorkAsync.SaveChangesAsync(); return RedirectToAction("CustomFields"); }
public async Task<ActionResult> CustomFieldUpdate(int? id) { MetaField field; if (id.HasValue) { var fieldQuery = await _customFieldService.Query(x => x.ID == id).Include(x => x.MetaCategories).SelectAsync(); field = fieldQuery.FirstOrDefault(); } else field = new MetaField() { Required = false, Searchable = false }; var categories = await _categoryService.Query().SelectAsync(); var model = new MetaFieldModel() { MetaField = field, Categories = categories.ToList() }; return View(model); }