I know this question may sound really dumb, but i'm trying to create something like a shopping cart. I have a list of products and an input field where the user sets the wanted quantity for each product. I'm a little bit blocked at how to get the input value when pressing the Add to Cart button and how to create a simple page where i see only the items with the wanted quantity. Any idea of how i can do these 2 things? Here is what i've managed to do so far, but for some reason the WantedQuantity input always comes as null in the controller.

Here is the View:

@using System.Linq
@model IEnumerable<ISS.Models.Drugs>

@{
    ViewData["Title"] = "Product List";
}

<h1>Product List</h1>
@*<p>
        <a asp-action="CheckCart">Check Cart</a>
    </p>*@

<table class="table">
    <thead>
        @*<tr>
            <th>
                @{
                    Html.RenderPartial("_HeaderNavBar");
                }
            </th>
        </tr>
        <tr>*@
            <th>
                @Html.DisplayNameFor(model => model.Name)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.ModificationData)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Quantity)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Price)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.WantedQuantity)
            </th>
            <th></th>
        </tr>
    </thead>
    <tbody>

        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.Name)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.ModificationData)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Quantity)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Price)
                </td>
                <td>
                    <form asp-controller="DrugsUser" asp-action="AddToCart">
                        <input type="text" value="@item.WantedQuantity" />
                        @*<input asp-for="@item.WantedQuantity" class="form-control"/>*@
                    </form>
                </td>
                <td>
                    <a asp-action="AddToCart" asp-route-id="@item.Id">Add To Cart</a> |
                    <a asp-action="Details" asp-route-id="@item.Id">Details</a>
                </td>
            </tr>
        }
    </tbody>
</table>

Here is my controller method:

        public async Task<IActionResult> AddToCart([Bind("Id,Name,ModificationData,Quantity,Price")] int WantedQuantity, int? id)
        {
            if (id == null)
            {
                return NotFound();
            }
            Order order = new Order();
            var drugs = await _context.Drugs.FindAsync(id);
            if (drugs.Quantity <= WantedQuantity)
            //    var drugs = await _context.Drugs.FindAsync(WantedQuantity);
            //if (drugs.Quantity > WantedQuantity)
            {
                return View("~/Views/Home/CartError.cshtml");
            }
            else
            {

                order.Name = drugs.Name;
                order.Quantity = WantedQuantity;
                order.Price = drugs.Price * WantedQuantity;
                order.ModificationData = DateTime.Now;
                _context.Orders.Add(order);
                await _context.SaveChangesAsync();
            }

            return View(await _context.Orders.ToListAsync());
            //return View();
        }

And here is my Order model:

    public class Order
    {
        public int Id { get; set; }

        [StringLength(60, MinimumLength = 3)]
        [Required]
        public string Name { get; set; }

        [Display(Name = "Last modified date")]
        [DataType(DataType.DateTime)]
        public DateTime ModificationData { get; set; }

        [Required]
        [Range(1, 100)]
        [Display(Name = "Quantity")]
        public int Quantity { get; set; }

        [DataType(DataType.Currency)]
        [Range(1, 300)]
        [Column(TypeName = "decimal(18,2)")]
        public decimal Price { get; set; }
    }

Any help will be highly appreciated.

0 Answers