Good Morning. Came across code implemented by someone else and I’m not understanding how the FormCollection is used. Is the FormCollection merely a collection of the controls on the form?
public Email(FormCollection fc)
{
StringBuilder sb = new StringBuilder();
this.fc = fc;
string[] keys = fc.AllKeys;
sb.Append("Category,\"REQUEST\",<br />");
sb.Append("Type,\"TEST EXPRS\",<br />");
}
In this particular code snippet it is not used and completely unnecessary as argument.
Personally I never use FormCollection in ASP.NET MVC applications. I always prefer to define a view model and have the controller action take this view model as argument. Its properties will be automatically populated by the default model binder. For example:
and then:
But back to your question:
FormCollectionrepresents a key value pair of all POSTed values. It is weakly typed and the values are always string meaning that you will have to manually convert them to their underlying type if you wanted to manipulate them. Try to avoid it.