J'ai une catégorieViewModel comme suit:
public class CategoryViewModel
{
public string Id {get; set;}
public string Name { get; set; }
public IEnumerable<SelectListItem> Products { get; set; }
public List<string> SelectedProductIds { get; set; }
}
La méthode GET de CatégoryController utilise cette catégorie ViewModel pour instancier un objet et ajoute tous les produits à cet objet CatégoryViewModel. Ensuite, il itère à travers tous les produits et définit la propriété sélectionnée des produits à TRUE qui sont incluses dans l'objet de catégorie:
public ActionResult CategoryController(string categoryId)
{
CategoryDbContext db = new CategoryDbContext();
CategoryRepository CategoryRepo = new CategoryRepository(db);
ProductRepository ProductRepo = new ProductRepository(db);
Category category = CategoryRepo.GetCategory(categoryId);
CategoryViewModel categoryView = new CategoryViewModel()
{
Id = category.Id,
Name = category.Name,
Products = from product in ProductRepo.GetAllProducts()
select new SelectListItem { Text = product.Name, Value = product.Id, Selected = false}
};
foreach (var product in category.Products)
{
categoryView.Products.Where(x => x.Value == product.Id).FirstOrDefault().Selected = true;
}
return View(categoryView);
}
En utilisant le débogueur, j'observe que ForEach s'exécute, mais CatégoryView a tous les produits avec une propriété sélectionnée toujours définie sur False.
Cependant, celui-ci fonctionne bien:
public ActionResult CategoryController(string categoryId)
{
CategoryDbContext db = new CategoryDbContext();
CategoryRepository CategoryRepo = new CategoryRepository(db);
ProductRepository ProductRepo = new ProductRepository(db);
Category category = CategoryRepo.GetCategory(categoryId);
CategoryViewModel categoryView = new CategoryViewModel()
{
Id = category.Id,
Name = category.Name,
Products = from product in ProductRepo.GetAllProducts()
select new SelectListItem { Text = product.Name, Value = product.Id, Selected = category.Products.Contains(product)}
};
return View(categoryView);
}
Quelqu'un peut-il expliquer la différence et pourquoi le premier ne fonctionne pas?
ÉDITER:J'utilise EF 6 et les produits et les catégories sont stockés dans la base de données avec une relation plusieurs à plusieurs.