Question

I'm currently poking around inside the FormCollection in MVC and wondering about the difference in meaning and expected usage between the AllKeys and the Keys properties. I know that AllKeys is a string[] and Keys is a KeysCollection but if you had to look through the form fields submitted during a post, why would you choose one property over the other? Is it just a convention to use AllKeys to quickly check for the existence of a specific key? I have multiple submit buttons on one of my forms and am using AllKeys to quickly check which was actually used to submit the form, but it got me thinking about the intended difference between the two, if indeed there is any.

Most of the time I'll be taking advantage of model binding and passing a ViewModel or Command object back to the controller action but are there specific use cases for FormCollection.Keys in particular that I'm not aware of? From a quick peek inside NameValueCollection and NameObjectCollectionBase it looks as though KeysCollection is actually simply a collection of strings, rather like a string[] in fact. Was one perhaps introduced in a later version of the framework and the other kept as well for backward compatibility?

Was it helpful?

Solution

Looks like this is a product of pure inheritance. Take a look at the inheritance diagram chart for FormsCollection (http://msdn.microsoft.com/en-us/library/system.web.mvc.formcollection(v=vs.108).aspx)

FormsCollection inherits from NameValueCollection which inherits from NameObjectCollectionBase. Keys are defined in NameObjectCollectionBase and inherited by NameValueCollection which defines AllKeys as a string representstion of all the keys. The sole purpose of NameValueCollection seems to be to expose keys as strings (MSDN: Represents a collection of associated String keys and String values that can be accessed either with the key or with the index)

May be there is more to it, but this is what I found so far for this one. Please post if you (or any one else) finds any other explanations

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top