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"})
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.