I have a problem that I frequently deal with using MVC that I can't seem to find an easy solution for.
Very frequently I find myself creating a viewmodel for an object that has a collection of smaller objects.
For instance,
I created a Staff screen where the user adds a staff member to the database. Most of this is fairly straight forward, but then I need to be able to add a credential.
The number of credentials a staff can have is dynamic, so there's a second table with a foreign key referencing the staff, as well as the appropriate credential data.
So my viewmodel has a Staff object, and an IEnumerable of Credential objects.
Currently when the user adds/edits staff in the partialview form, a popup with the credential fields appears. When you save the credential, I manually generate the hidden fields of every single property as hidden html elements using javascript strings, then append it to the appropriate places. It gets really unmanageable.
var hid0 = "<input name=CurrentCredentials.Index type=hidden value='i-" + statid + "'>";
var hid1 = "<input name=CurrentCredentials[i-" + statid + "].StatID id='credentials-statid-" + statid + "' type=hidden value='" + statid + "'>";
...etc to hid7. I also generate a series of tags to display the data on the page. An absolute mess.
It currently works, but there has to be a better way, and I'm about to deal with a very similar problem. I feel like I'm reinventing the wheel. My first instinct was to add a nested form, but my research tells me MVC forbids this, so that's out of the question.
To sum up what I'm looking for: How can I dynamically load/save objects in the partial view, without losing the data on HTTPPOST?