I try to remove item from products in DeleteProduct method and Remove method returns true but all items still in list and I can't figure out why.
As I understend products list creates at the first start of the application and at static constructor it fills with items. Then in DeleteProduct method one of the items removes and executes Products Method.
Please someone explane it!
public class ProductController
: Controller
{
private static IEnumerable<ProductViewModel> products = null;
static ProductController()
{
products = new List<ProductViewModel>
{
new ProductViewModel{Id = 0, Description = "Milk", Price = 21.0, IsAvailable = true, LastUpdate = DateTime.Now},
new ProductViewModel{Id = 1, Description = "Bread", Price = 10.10, IsAvailable = false, LastUpdate = DateTime.Now},
new ProductViewModel{Id = 2, Description = "Soure creame", Price = 34.5, IsAvailable = true, LastUpdate = DateTime.Now},
new ProductViewModel{Id = 3, Description = "Chocolate", Price = 31.0, IsAvailable = true, LastUpdate = DateTime.Now},
new ProductViewModel{Id = 4, Description = "Apples", Price = 1.0, IsAvailable = true, LastUpdate = DateTime.Now},
};
}
public ActionResult Products()
{
return View(products);
}
public ActionResult DeleteProduct(int id)
{
var product = products.FirstOrDefault(p => p.Id == id);
if (product != null)
products.ToList().Remove(product);
return RedirectToAction("Products");
}
}