Array asp dynamically in updating 2 of us dating service
Things get even trickier when the number of input controls for each item increases- you have to keep all these input controls in sync so each item gets saved correctly. NET MVC has a built in feature to pull out a list of complex types posted to a page and automatically put them in a model.
We’re going to combine this feature with j Query to dynamically add form elements on a page which end up in a list of complex types that can be accessed in a controller action.
Essentially, each set of related input elements are grouped together using a uniquer key. The key we specify for the element will be the key we use in the name attribute the input element.
The key can be any string- it doesn’t have to be an integer index. In our sample, we want our list to end up in the my List. So for every object in the Items list, we need a hidden input field with a name of “my List. If our key is “foo”, and our property is “Quantity”, we’ll have: [/pre] Even though we couldn't do that in c#, we can do it in our markup.
NET MVC 2 Futures library that handles sending JSON to an action method but I just never found time to blog about it.
There’s one key problem with using a model binder to accept JSON.
Rather than write a custom model binder which was the approach I took, Jonathan had the unique insight to write a custom value provider which received JSON data and serialized it to a dictionary rather than the target object.
And as you can see in the Fiddler screenshot, I sent an invalid Age to the server and yet, it all still works. I can finally cross this off of my immense blog backlog. I hope you find this useful and as always, let me know if there are ways we can improve it!
:) Hopefully soon, I’ll blog a more detailed write-up of value providers. Oh, and here’s Javier’s updated sample with the value provider.
You want the user to add as many “items” as they want to the list, then save the entire list at once.
Dynamically adding elements to a page is easy with j Query, but parsing out list items on the server can be difficult- especially when you don’t know how many items are on the page! I ended up using a JSON plug-in for j Query I found on the Internets.