1

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.

tereško
  • 56,151
  • 24
  • 92
  • 147
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – VDWWD Jan 26 '18 at 18:31
  • 1
    Check what is actually rendered in the markup for this `` – npearson Jan 26 '18 at 18:36
  • I seem to be able to pass a string property of the same item, but not the item itself. – Hellowuvrld Jan 26 '18 at 18:39
  • 2
    Right, you cannot put an entire complex object into a hidden field. Change your hidden field to be `@item.ProductId` and change the action to accept the id instead of the object. – npearson Jan 26 '18 at 18:41
  • Then I guess passing the ID is the solution. Thanks. – Hellowuvrld Jan 26 '18 at 20:12
  • please edit your answer to include all of the code in the view. I need to see how you are linking from your view to your model. – Michael Henderson Jan 26 '18 at 22:04

2 Answers2

0

You are trying to pass an object in a hidden form field. What would that look like ?

Instead, set all data fields your Product model needs to form fields, for example hiddenFor. Controller will then read the fields and bind them to the model.

Niko
  • 418
  • 3
  • 9
0

Like I said in the comment, a better approach than trying to pass the entire object is to just use the product Id.

<input type="hidden" name="productId" value="@item.ProductID" />

Then modify your controller action accordingly. I recommend putting a GetProductById() method on your DAL if possible.

[HttpPost]
    public ActionResult Purchase([FromBody] int productId)
    {
        ProductDAL dal = new ProductDAL();
        Product item = dal.GetProductById(productId)

        if (item.Quantity == 0)
        {
            Session["check"] = item.ProductName;
            return View("PurchaseFail");
        }

        item.Quantity--;
        dal.SaveChanges();
        return View("PurchaseSuccess", item);
    }
npearson
  • 652
  • 4
  • 17