Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Html.BeginForm RouteValueDictionary vs Html.Hidden

Tags:

asp.net-mvc

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.

like image 211
abenci Avatar asked May 01 '26 20:05

abenci


1 Answers

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.

like image 88
Alexander Manekovskiy Avatar answered May 03 '26 18:05

Alexander Manekovskiy