I've successfully selected the Objects I want to delete. But the problem is when I remove an item from Object array, it doesn't make any changes. My code is following below..
My database
public List<Product> db = new ProductRepository().GetProducts();
Here it shows all the products with checkbox..
public ActionResult MultipleDeletes()
{
return View(db);
}
On Submitting "Button named Delete", I got problem.
[HttpPost]
public ActionResult MultipleDeletes(int[] selectedProducts)
{
var del_products = from x in selectedProducts
from y in db
where y.ProductId == x
select y;
foreach (var item in del_products)
{
//Product p = db.Find(item.ProductId);
//db.Remove(p);
//db.SaveChanges();
}
return View(db);
}
Could anyone help me? can you also tell me, how to write Lambda expression instead of LinQ?
The problem was in the model. I used NBuilder.. so it didn't really save the data. I built an DbContext. Then it worked. Solution is ..
public ProductDBContext db = new ProductDBContext();
[HttpPost]
public ActionResult MultipleDeletes(int[] selectedProducts)
{
foreach (int item in selectedProducts)
{
Product product = db.Where(p => p.ProductId == item).SingleOrDefault();
db.Remove(product);
db.SaveChanges();
}
return View();
}