I have recently been spending quite a bit of time on StackOverflow and I noticed that quite a few of the MVC-related answers make use of the FormCollection class. If you’re unfamiliar with this class, here is an example of how it gets used:
Which of course works exactly as expected. Let’s take a look at the same example using a view model.
Which I think is a lot neater (plus no magic strings). I find that this approach is much easier to test and maintain.
Keep in mind that you can even create your own model binder for specifying how the form values should bind to the view model. (More on this in a later post). However in most cases the default model binder should be sufficient.