What's the difference between the two methods below to pass parameters to a controller action? When should I use one or the other?
First approach:
@using (Html.BeginForm("ActionName", "ControllerName", new {orderId = Model.orderID, productId = Model.productID}, FormMethod.Post))
{
...
}
Second approach:
@using (Html.BeginForm("ActionName", "ControllerName", FormMethod.Post))
{
...
@Html.Hidden("orderId", model.orderID)
@Html.Hidden("productID", model.productID)
}
Thanks.
The first approach is using FormExtensions.BeginForm method override which is appending values to the form submit url:
<form action="/ControllerName/ActionName?orderId=<Model.orderID>&productId=<Model.productID>" action="post">
Parameters from here could be retrieved from the route parameters collection.
Second approach will simply add two hidden fields which could be bound to the model object passed in and out of controller action.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With