valheru
valheru

Reputation: 143

c# IDictionary dropdown httpPost

I have a page with af form with a dropdownlist with products in it and on that page is also some info about the order. Thats why I made a viewmodel to send the products and the orders to the htmlpage. I want to submit the selected product from the dropdownlist (its the product id) and the hidden orderid, but when I click the button my page does not work. It is not submitting the data to the controller and it does not reach the breakline.

My code:

Controller

 public ActionResult ChangeOrder(int orderId)
    {
        var order = _db.Orders.Where(o => o.OrderId == orderId).FirstOrDefault();
        var productCategory = _db.Products.Where(p => p.ProductId == order.ProductId).Select(c => c.CategoryId).First();

        var producten = _db.Products.Where(p => p.CategoryId == productCategory && p.Availability == true).ToDictionary(p => p.ProductId, p => p.Description);

        return View(new OrderProductViewModel(producten, order));
    }

    [HttpPost]
    public ActionResult ChangeOrder(OrderProductViewModel key)
    {
        var test = key;
        return null;
    }

Part of HTML page

 <div class="panel panel-default">
        <div class="panel-body">
            @using (Html.BeginForm("ChangeProduct", "Order", FormMethod.Post, new { role = "form" }))
            {
                @Html.Hidden("orderid", Model.Order.OrderId)
                    <div class="form-group">
                        @Html.DropDownListFor(
                            m => m.Products, new SelectList(Model.Products, "Key", "Value"), "select product", new { @class = "form-control" })
                    </div>
                    <input type="submit" value="Wijzig" class="btn btn-primary" />
            }
        </div>
    </div>

ViewModel

 public class OrderProductViewModel
{
    public IDictionary<int, string> Products { get; set; }
    public Order Order { get; set; }

    public OrderProductViewModel(IDictionary<int, string> products, Order order)
    {
        this.Products = products;
        this.Order = order;

    }
}

What am I missing?

Upvotes: 0

Views: 28

Answers (1)

valheru
valheru

Reputation: 143

Thanks to mjwills to pointing out that ChangeProduct should be ChangeOrder at Html.BeginForm. That was my first mistake.

Second:

This is my fixed constructor. I get the right parameters atm.

    [HttpPost]
    public ActionResult ChangeOrder(int orderid, int Products)
    {
        //implementation here
        return null;
    }

Upvotes: 1

Related Questions