public async Task <ActionResult> EditCategory(string productName, ThreadCategoryViewModel formModel) { if (this.ModelState.IsValid) { await this.productService.SaveCategory(formModel.ProductId, formModel); return(this.RedirectToRoute(RouteConfig.PRODUCT_ACTIONS, new { productName = productName, controller = "Settings", action = "Categories" })); } var viewModel = await this.GetSettingsViewModel(productName); viewModel.Category = formModel; viewModel.ActiveTab = SettingsActiveTab.Categories; return(this.View("EditCategory", viewModel)); }
public async Task SaveCategory(int productId, ThreadCategoryViewModel viewModel) { ThreadCategory category; if (viewModel.Id.HasValue) { category = await this.Db.Products .Where(x => x.Id == productId) .SelectMany(x => x.Categories) .FirstOrDefaultAsync(x => x.Id == viewModel.Id.Value); } else { category = new ThreadCategory(); this.Db.ThreadCategories.Add(category); } category.Name = viewModel.Name; category.ThreadType = viewModel.ThreadType; category.ProductId = productId; await this.Db.SaveChangesAsync(); }