ASP.NET MVC Tip #4 - Handling Multiple Form Actions on One View
You have a view which renders a list of customers. You need to provide a way to delete customers from this table view of the data. In your "CustomersController" you have a Delete action which takes a Customer ID. How do we go about providing the user with a simple interface where multiple actions can be posted from one view?
In a typical web forms based ASP.NET application, we're used to having one form per page. In the MVC framework, we're in ful control of the HTML rendered which means we can have more than one form on the page. This means that each form can post to a different action. Assume we have a collection of Smurfs which will be rendered on a view. This ViewPage will use a strongly typed ViewData with the list of Smurfs. As we're rendering the view, we simply need to set the parameters of the form accordingly.
At this point, we simply handle the controller action as any other form. The actual HTML output looks like this. Notice how the action uses the item.ID.