MVC 5 post serializing issue when name value pair is missing within the collection

47 Views Asked by At

I recently ran into a problem within my MVC5 C# project where my dynamically generated HTML elements (a collection of phone numbers) could be posted to the server where indexes were missing. Like this, missing [1]:

ClientInfo.Phones[0].PhoneNumber
ClientInfo.Phones[2].PhoneNumber
ClientInfo.Phones[3].PhoneNumber

When the page has more than one to add, or if a user manually adds another, the incremental numbering increases for the dynamic elements. But when a user deletes, say dynamic element [1], then there's a missing element in the collection. When the form is posted to the server the method that receives expects a class called ClientInfo, with an IEnumerable property, among lots of other stuff. When they index values are in order (0,1,2,3,...) everything serializes as expected into this Phones collection property. When one is missing, as in the example above, only those that are numbered, in order from 0 will serialize. In this case, index 2, 3 wouldn't be included, only [0].

I can see all the values and their index number in Fiddler when posting the form, so I know they're coming across the wire correctly. Is there a way to serialize these items despite them not having all the collection elements? Any help is appreciated! Thank you!

0

There are 0 best solutions below