I have a view which I load with product data. When I press the 'Add to Basket' button I'd like the same page to reload again but I'm getting errors such as:
Object reference not set to an instance of an object.
View:
@model List<Ecommerce.Models.HomeModels.Product>
@foreach (Ecommerce.Models.HomeModels.Product product in Model)
{ // above error points here!!!!!!!!!!!
using (Html.BeginForm())
{
<input type="hidden" name="productId" value="@product.ID" />
<input type="submit" value="Add to Basket"/>
}
}
Controller:
public ActionResult BuyProducts()
{
List<Models.HomeModels.Product> products = new List<Models.HomeModels.Product>();
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
//Method to load data into products
}
}
TempData["products"] = products;
return View(products);
}
[HttpPost]
[AllowAnonymous]
public ActionResult BuyProducts(string productID)
{
string id = productID;
return View(TempData["products"]);
}