So I have this CSHTML:
@using (Html.BeginForm("DeleteProduct", "Shop", FormMethod.Post))
{
@Html.Hidden("productId", product.ProductId);
@Html.Hidden("productVariantId", product.ProductVariantId);
@Html.Hidden("quantity", product.Quantity);
<button type="submit" class="btn btn-default"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span></button>
}
That calls this backend:
[HttpPost]
public ActionResult DeleteProduct(int productId, int? productVariantId, int quantity)
{
//...product gets deleted, etc. Not important for this question
//regenerate the view, same as when loading the 'Order' view initially
OrderWrapperViewModel model = GenerateOrderWrapperModel();
return View("Order", model);
}
loading Order view in controller looks like this:
[HttpGet]
public ActionResult Order()
{
OrderWrapperViewModel model = GenerateOrderWrapperModel();
return View(model);
}
When I first call delete, I delete a product with ID: 7
and a product variant ID: 8
. All goes fine. But after the page is returned, product with ID 7 is not listed anymore, only product with ID 1. So the second time it should be product ID: 1
and product variant ID: 4
.
When returning from DeleteProduct
, the url is currently showing /Shop/DeleteProduct. When I debug through the razor code:
Seems correct, right?
Well, the html is different:
Of course, clicking the button passes those incorrect parameters as well. I can assure you that this is the same button, there is only 1 product shown at that time.
I solved it by simply redirecting to Order view again, so I am more interested in why this happens. Reason is, because for others I am returning a error message (using ModelState
). That of course doesn't work when I redirect.
It also happens in production environment.