public void Add(ItemModel Item, List<CategoryModel> Categories) { var item = new ItemEntity() { Number = Item.Number, Name = Item.Name, Price = Item.Price, Description = Item.Description }; var CategoryEnt = new List<CategoryEntity>(); foreach (var category in Categories) { var categoryE = context.CategoryEntities.Where(cat => cat.Id == category.Id).First(); if (categoryE != null) CategoryEnt.Add(categoryE); } /*var CategoryEntities = context.CategoryEntities.Select(category => Categories .Where(categoryEntity => categoryEntity.Id == category.Id)).ToList();*/ item.Categories = CategoryEnt; context.ItemEntities.Add(item); context.SaveChanges(); }
public ActionResult AddItem(AdminPageModel PostedPage) { var CategoriesList = _categoryService.GetSelectedCategory(PostedPage.taggles); var Item = new ItemModel() { Number = PostedPage.NewItem.Number, Name = PostedPage.NewItem.Name, Price = PostedPage.NewItem.Price, Description = PostedPage.NewItem.Description }; _itemService.Add(Item, CategoriesList); return RedirectToAction("Index", "Article"); }