This is somewhat a two-part question (please let me know if they should be split up).
1) I have a model class with an array of objects contained inside it. I would like to be able to bind this automatically so I can accept a single pollModel
argument in my controllers.
public class pollResponseModel { public long id { get; set; } public long pollID { get; set; } public string text { get; set; } public long count { get; set; } } public class pollModel { public long id; public long entID { get; set; } public string question { get; set; } public DateTime posted { get; set; } public DateTime expiration { get; set; } public pollResponseModel[] responses { get; set; } }
The problem is that I'm not sure how to bind the responses
field, seeing as it can be any arbitrary size. Well, I can bind it properly when displaying the edit view, but that's about it. That leads me to the second part of my question:
2) What's an acceptable way of dynamically creating and removing data in a list on the client, so that it can be bound to a model and accessed in its modified form on the server? I envision the creation/removal process working like the iPhone list GUI: a single +
button will add a new element, and a -
button on each row of data will remove it from the list. I would imagine jQuery is an appropriate starting point but my JS skills are very limited.
Check out this article by Phil Haack : Model Binding To a List. It explains exactly what you need to do to bind to list properties, or properties that are complex objects.
Essentially you just have to construct your POST data in the correct way for the model binder to parse it. The article explains how to add hidden index fields and represent your complex properties in your form.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With