I have went over many similar posts, cant seem to find the reason for my error.
Im trying to pass an object type "Product" from a view to a controller, then check for this item's properties.
the view looks like this -
<table width="100%" cellpadding="5" cellspacing="2" border="0" style="background-color: White;">
@foreach (var item in Model.products)
{
<tr>
<td>@item.ProductName</td>
<td>@item.Price</td>
<td>@item.Quantity</td>
<td><text><img src="@item.Image" width="250px" height="200px"></img></text></td>
<td>
@using (Html.BeginForm("Purchase", "Home", FormMethod.Post, new { Product = item}))
{
<input type="hidden" name="item" value="@item" />
<input type="submit" value="Purchase" onclick="return confirm('Purchase @item.ProductName ?')" />
}
</td>
</tr>
}
</table>
And the controller -
[HttpPost]
public ActionResult Purchase(Product item)
{
if (item.Quantity > 0)
{
ProductDAL dal = new ProductDAL();
foreach (Product obj in dal.Products)
{
if (obj.ProductID.Equals(item.ProductID))
{
obj.Quantity--;
}
}
dal.SaveChanges();
return View("PurchaseSuccess", item);
}
Session["check"] = item.ProductName;
return View("PurchaseFail");
}
The program stops at if(item.Quantity > 0)
due to mentioned error.