I\'ve inherited a code base written in ASP.Net MVC 4. Every post method takes a FormCollection
. Aside from annoyance of having to access the values through quot
Is there any good reason to use FormCollection instead of ViewModel?
No. I have following issues.
Issue - 1
In case FormCollection
is being used...It will be mandatory to Type Cast
the Primitive Type
Values un-necessarily because while getting the entry of specific Index of the System.Collections.Specialized.NameValueCollection
, value being returned is of type String
. This situation will not come in case of Strongly Typed View-Models
.
Issue - 2
When you submit the form and goes to Post
Action Method, and View-Model
as Parameter exists in the Action method, you have the provision to send back the Posted Values to you View
. Otherwise, write the code again to send back via TempData/ViewData/ViewBag
View-Models are normal classes, created to bind data to-from Views
Issue - 3
We have Data Annotations that can be implemented in View Model
or Custom Validations
.
ASP.Net MVC simplifies model validatons using Data Annotation. Data Annotations are attributes thyat are applied over properties. We can create custom validation Attribute by inheriting the built-in Validation Attribute class.
Issue - 4
Example you have the following HTML
<input type="text" name="textBox1" value="harsha" customAttr1 = "MyValue" />
Question : How can we access the value of customAttr1 from the above eg from inside the controller
Answer : When a form get posted only the name and value of elements are posted back to the server.
Alternatives : Use a bit of jQuery to get the custom attribute values, and post that along with the form values to action method
Another option is to rather put what you got in your custom attributes in hidden controls
That's the reason, I would always prefer to use View-Models
The only advantage I can think of is if you want to use the automatically generated controller provided when you don't specify a EF model to be strongly typed to. In that case, your Create and Edit actions will use the FormCollection object as it is a reliable, pre-existing artifact of the framework to work with for this purpose. Perhaps the previous developer chose this option while creating his controllers, and stuck with it since Visual Studio must know what it's doing :)
But, in reality, I would never recommend this headstart of a few seconds. It's always better to build out viewmodels, I would recommend looking at the effort to move in that direction if only for maintenance purposes. With model binding and strongly typed views and html helpers, you are much more likely to reduce the number of run-time errors as a result of changing some magic string and not realizing it until your page blows up.
Yes. Sometimes, it can be useful. Here's an example:
Let's say we have in our db "date_and_time_field
".
In Razor View, we want to use two form fields. The first one "Date" (maybe with jQuery UI Datepicker). The second one "Hour".
In the Controller Action, we compose the "date_and_time_field" by means of Request.Form["Date"]
and Request.Form["Hour"]
.
There are other scenarios where it can be useful:
A cross-table (with checkBoxes in Razor view)
The collection Request.Unvalidated().Form
(maybe this is not part of your question: I don't wanna be off-topic)
Responding to the title question: yes.
There are some situations that FormCollection
needs to be used. For instance, suppose a ViewModel
that has a property that implements the 1 to N relation (in concrete case, a TimesheetViewModel
with ICollection<TimesheetEntryViewModel>
), and the Controller has to perform a validation between the time entries to not get a time collision between the end time of an entry and the start time of the following entry. To mark a related entry with a validation error, how can be the line index be retrieved?
Well, with the default model binding, the index value is lost in the Controller logic. Fortunately, FormController
stores the index you used in the View and a more specific validation can be done.
The default model binder will do almost everything you need it to do. I resorted to the FormCollection
once - only to later figure out how to bind arrays of elements into a collection on the ViewModel.
Just go ViewModel. Better all around, for every reason enumerated.