Using named parameters as controller input versus FormCollection

asp.net-mvc

Solution

Another solution is to use models instead of manipulating the raw values. Like this:

class MyModel
{
  public string ItemOne { get; set; }
  public int? ItemTwo { get; set; }
}

Then use this code:

public ActionResult MyAction(MyModel model)
{
  // Do things with model.

  return this.View(model);
}

In your view:

<%@ Page Inherits="System.Web.Mvc.ViewPage<MyModel>" %>
<%= Html.TextBox("ItemOne", Model.ItemOne) %>
<%= Html.TextBox("ItemTwo", Model.ItemTwo) %>

Problem

I'm new to ASP.NET MVC so this could have an obvious answer. Right now I have a form in my view with a lot of input controls, so I have an action that looks like this: ``` public ActionResult MyAction(string formItemOne, int? formItemTwo, etc...) ``` It has like a dozen parameters, which is pretty ugly. I'm trying to change it to this: ``` public ActionResult MyAction(FormCollection formItems) ``` and then parse the items dynamically. But when I change to a FormCollection, the form items no longer "automagically" remember their values through postbacks. Why would changing to a FormCollection change this behavior? Anything simple I can do to get it working automagically again? Thanks for the help, ~ Justin

Original source