]> MVC5 Default values in a Dictionary 🌐:aligrant.com

MVC5 Default values in a Dictionary

Alastair Grant | Mon 21 Mar 2016

I encountered a strange bug where there was an unexpected value in a Dictionary object that was being bound from a form post. The dictionary had two null entries keyed, "controller" and "action". Switching my Dictionary to take strings as vaules too displayed they were being assigned the path routing information. And it didn't matter where I placed the dictionary (or what I called it) in the method signature.

This seems to be related to passing in of path information by default, and the cause was the form didn't have any values to post in this scenario, so there was no mapping taking place and the routing information was being put in instead.

I believe in MVC6 you'll be able to use the [FromForm] attribute on your Action's parameters. In the mean time, you need to work around this by providing an empty placeholder entry in your form (e.g. @Html.Hidden("MyDictionary")).

Remember to sanitise your inputs.

Breaking from the voyeuristic norms of the Internet, any comments can be made in private by contacting me.