I am following a book and here is the code:
@using (Html.BeginForm("RemoveFromCart", "Cart"))
{
@Html.Hidden("ProductId", line.Product.ProductID)
@Html.HiddenFor(x => x.ReturnUrl)
<input class="btn btn-sm btn-warning" type="submit" value="Remove" />
}
And here is also his explanation for why he has used Hidden
instead of HiddenFor
but still I can't understand the wiring behind it that he is talking about. Can you elaborate this a little more ?
public RedirectToRouteResult RemoveFromCart(Cart cart, int productId, string returnUrl)
You need to remember that these helpers are just ways of generating HTML markup.
Example of the generated markup:
@Html.Hidden("ProductId", line.Product.ProductID)
Generates:
<input type="hidden" name="ProductId" value="5" />
@Html.HiddenFor(x => x.Product.ProductID)
Generates:
<input type="hidden" name="Product_ProductId" value="5" />
Your controller defines a parameter named productId
. In order for model binding to work, the name
value of the hidden input must match the argument name.
Product_ProductId
will not match the defined argument productId
for the RemoveFromCart
Controller Action.
It is worth noting that model binding is case insensitive. So your hidden input value of ProductId
will still bind to the RemoveFromCart
parameter of productId
.