3

I am fairly new to programming and i got stuck with that problem below.

I am trying to pass 2 values to my controller, "Id" and "quantity".

"id" is hitting the controller as intended. but i can not say the same for the "quantity".

Basically, the quantity is/should be a textbox where user add a quantity. The result for quantity is hitting the controller as NULL or 0 (zero).

That will be used for a shopping cart i am trying to set up. Where i get the ID for the product, and quantity.

I have tried using razor, but the data entered by user for the quantity is not passing to controller. I am sure i have done passing 2 parameteres like this before and i saw some example like that as well. But i am frustrated that i can not do a simple thing like that now. :(


public class OrderViewModel{
        public IEnumerable<Product> Product { get; set; }
        public IEnumerable<Supplier> Supplier { get; set; }

        public Product product { get; set; }
        public Supplier supplier { get; set; }      

        public int Quantity { get; set; }}}```

Controller

public ActionResult AddToCart(int id, int? qty)
{
}

My view

@using (Html.BeginForm("AddToCart", "Order", FormMethod.Get))
{
    <table class="table table-hover table-striped table-bordered">
        <tr>
            <th> @Html.DisplayNameFor(model => model.product.ProductCode) </th>
            <th> @Html.DisplayNameFor(model => model.product.Description) </th>
            <th> @Html.DisplayNameFor(model => model.product.Image)       </th>
            <th> @Html.DisplayNameFor(model => model.product.Price)       </th>
            <th> Quantity for purchase                                    </th>
            <th> @Html.DisplayNameFor(model => model.supplier.CompanyName)</th>
            <th></th>
        </tr>

        @foreach (var item in Model)
        {
            <tr>
                <td>  @Html.DisplayFor(modelItem => item.product.ProductCode)               </td>
                <td>  @Html.DisplayFor(modelItem => item.product.Description)               </td>
                <td>  @Html.DisplayFor(modelItem => item.product.Image)                     </td>
                <td>  @Html.DisplayFor(modelItem => item.product.Price)                     </td>
                <td>  <input type="number" value="@item.Quantity" />  </td>
                <td>  @Html.DisplayFor(modelItem => item.supplier.CompanyName)              </td>
                <td>
                    @Html.ActionLink("Add", "AddToCart", new { id = item.product.ProductId, qty = item.Quantity }, new { @class = "btn btn-success"})                 
                </td>
            </tr>
        }

    </table>
}

My expectation is that the "quantity" entered by the user, will hit the action on the controller.

**I fixed that by adding that following**

<input type="hidden" name="id" value="@item.product.ProductId" />
<input type="submit" value="Add">

removed the following

 @Html.ActionLink("Add", "AddToCart", new { id = item.product.ProductId, qty = item.Quantity }, new { @class = "btn btn-success"}) 

Blindfaith
  • 33
  • 5

2 Answers2

1

If you want it to work after form post:

<input type="number" value="@item.Quantity" />

Change to:

<input name="qty" type="number" value="@item.Quantity" />

Please note the Controller name and action name in your Form tag (/Order/AddToCart) and the ActionLink (/AddToCart/Add). They are different and might not going to be the same Controller's Action.

FYI - Instead of an ActionLink you should have a submit button in there to post the form:

<input type="submit" value="Add">

You already have the route and the data ready in the form. An ActionLink would navigate to the action not post the form.

Rahatur
  • 3,147
  • 3
  • 33
  • 49
  • Thanks **Rahatur**, appreciate the help. Tried that before and for some odd reason it didnt work. – Blindfaith Jun 12 '19 at 11:27
  • @Blindfaith I have updated the answer. Please give that a try? – Rahatur Jun 12 '19 at 11:32
  • Thanks **Kumar**, based on that i added a new line for the ID as well. Hidden field. So now it is going through. It was showing as null, so now it works. Thanks – Blindfaith Jun 12 '19 at 12:14
0

you are missing the name property.it should be something like this.

<input type="number" name="qty" value="@item.Quantity" />

Note:- value of the name property should match the parameter name of the action in the controller.

Janmejay Kumar
  • 309
  • 2
  • 7
  • Thanks **Kumar**, thats what breaks my heart. I tried that but it somehow doesnt work. Very weird. Restarted Visual studio, etc.. And someone posted here and somehow it was deleted but he said that the page load with the value of Zero. Adn it doesnt matter what user enters, it will always be zero. That is what is happening. But there must be something wrong somewhere. Thanks for reading it. – Blindfaith Jun 12 '19 at 11:44