Why is only part of the object passing to the home controller?

61 views Asked by At

From the menu page a user clicks an "Add to Order" button. The "Add to Order" button triggers the addToOrder function. That function works correctly, the drink is passed and an order is created with the drink information. After clicking the "Add to Order" button the user is sent to a confirmation page where their drink order is displayed and takes in their name in a form. The information passes correctly to the confirmation page, however when the form on the confirmation page is submitted the drink information is not passed but the rest of the form is. Any suggestions on how to get the drink information to pass in the object is greatly appreciated!

In the Home Controller:

From the menu page, this gets the drink information and works correctly:

 [HttpGet]
    public ActionResult addToOrder(int id)
    {
     
        Drink drinkToAdd = _drinkRepository.GetDrink(id);
        List<Drink> dList = new List<Drink>();
        dList.Add(drinkToAdd);
        Order order = new Order();
        order.drinkList = dList;
        order.total = drinkToAdd.drinkPrice;
        ViewData.Model = order;
        return View("OrderConfirmation");
    }

This method gets the information from the form in the order confirmation page:

 [HttpGet]
    public ActionResult confirmedOrder(Order order, int drinkID)
    {
        Drink drinkToAdd = _drinkRepository.GetDrink(drinkID);
        List<Drink> dList = new List<Drink>();
        dList.Add(drinkToAdd);
        order.drinkList = dList;
        _orderRepository.Add(order);
        return View("ThankYou");
    }

This is the order confirmation page:

 @model BartenderApp.Models.Order

@{ 
    ViewData["Title"] = "Order Confirmation";
}
   

<html>
<body>
    <h1>Order Confirmation</h1>
    <form asp-action="confirmedOrder" method="get" >
        <p>@Html.DisplayNameFor(m => m.FirstName): @Html.TextBoxFor(m => m.FirstName)</p>
        <p>@Html.DisplayNameFor(m => m.LastName): @Html.TextBoxFor(m => m.LastName)</p>

        <table class="table">
            <thead>
                @foreach (var item in Model.drinkList)
                {
                <tr>
                    <td>
                        @Html.HiddenFor(modelItem => item.id)
                    </td>
                    <td>
                        <b>@Html.DisplayNameFor(modelItem => item.drinkName)</b>
                    </td>
                    <td>
                        <b> @Html.DisplayNameFor(modelItem => item.drinkDescription)</b>
                    </td>
                    <td>
                        <b> @Html.DisplayNameFor(modelItem => item.drinkPrice)</b>
                    </td>
                </tr>
                }
            </thead>
            <tbody>
                @foreach (var item in Model.drinkList)
                {
                <tr>
                    <td>
                        @Html.HiddenFor(modelItem => item.id, new { drinkID = item.id } )
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.drinkName)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.drinkDescription)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.drinkPrice)
                    </td>>
                </tr>
                }
            </tbody>
        </table>
        <p style="float:left">@Html.ActionLink("Modify Order", "Menu")</p>
        <p style="float:right"><b>@Html.DisplayNameFor(m => m.total):</b> [email protected](m => m.total)</p>

        <input type="submit" value="Checkout" name="checkout" style="float:right" />
    </form>
</body>

</html>

When I hit the submit button on the form I thought the entire object would pass but only the first and last name are. I have tried to then pass the specific drinkID so I could locate it in the repository and add it to the order. However, the drinkID is not passing either (always 0). Why is the drink information not passing with the rest of the object?

Thanks

This is the URL after I hit the submit button on the order confirmation page: https://localhost:44378/Home/confirmedOrder?FirstName=John&LastName=Jones&item.id=1&item.id=1&checkout=Checkout

In this example, I did add the drink with the ID of 1 to my order. There is also another order in the repository, so the ID of the order is 2.

0

There are 0 answers