Fix MVC 5 Dictionary Keys Disappearing During Form Binding
A settings form shows multiple choices but the controller receives an empty dictionary or duplicate keys.
Applies to ASP.NET MVC 5 on .NET Framework. Use a staging or development copy to reproduce the behavior and inspect the actual response before changing production.
Diagnostic flowchart
Isolate the cause
Are inputs named as indexed key and value pairs?
- Yes. Missing partners or duplicate keys can invalidate the shape the binder expects.
- No. Unrelated HTML input names do not become dictionary pairs simply because they are displayed together.
Apply the fix
Post a collection of typed input rows, then validate allowed keys and duplicates before building a dictionary. Keep the UI ordering separate from dictionary semantics.
<input name="Entries[0].Key" value="Colour" />
<input name="Entries[0].Value" value="Blue" />
<input name="Entries[1].Key" value="Layout" />
<input name="Entries[1].Value" value="Compact" />Verify the result
- Expected path. Submit both rows and check the intended keys appear once each.
- Regression check. Submit a duplicate key and make sure the action returns useful validation rather than discarding a row.
Microsoft reference for binding edge cases · ASP.NET PB Tutorial Index
Comments
Post a Comment