John Stuart
John Stuart

Reputation: 333

ASP.NET MVC 2 Submitting a form and a parameter to controller action

Is it possible to submit a form on a view to the controller with a parameter?

My controller action is:

public ActionResult Index(BusinessObject busObj, int id = 0){
    return RedirectToAction("Index", new {businessObj = busObj, search = id });
}

I have a submit button, but i also have dropdownlists that post back to the controller so that the values can be filtered. I am trying to distinguish between the events using the id parameter. My intuition tells me that this involves routing, but im not sure what approach to take. Insight is welcome :D

Upvotes: 3

Views: 4961

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038730

Your question is not very clear. A form already contains parameters that will be sent to the controller action. So as long as you include the id parameter either at the form action or inside the form, it's value will be sent.

Example as route parameter:

<% using (Html.BeginForm("Index", "Home", new { id = "123" })) { %>
    ...
<% } %>

And as input field:

<% using (Html.BeginForm("Index", "Home", FormMethod.Post)) { %>
    <%= Html.Hidden("id", "123") %>
<% } %>

Upvotes: 2

Related Questions