I am extending a legacy Web Forms application to include an itemized list of form fields, with the ability to add/remove field rows on the client side before submitting. I've verified that I'm able to post and retrieve the data via Request.Form[...]
, but couldn't find a best practice for doing this that would deserialize into a collection on the server side (also open to suggestions on ways others have done this).
In essence, what I'm trying to do is this (pseudo code):
<!-- Fields generated with JavaScript -->
<li><input type="text" id="items[0].firstName" name="items[0].firstName" value="John" /></li>
<li><input type="text" id="items[0].lastName" name="items[0].lastName" value="Smith" /></li>
<li><input type="text" id="items[1].firstName" name="items[1].firstName" value="Mary" /></li>
<li><input type="text" id="items[1].lastName" name="items[1].lastName" value="Smith" /></li>
And have the result end up in a collection in .NET, like so (pseudo code):
var items = Request.Form["items"] as List<Person>;
or, even
var items = Request.Form["items"] as dynamic;
Perhaps I'm asking too much of .NET. I know I can handle this by creating fields like item_0_firstName
, item_0_lastName
, and parse through the form collection on postback, but I was hoping there was some way of serializing/deserializing collections from HTML to .NET that was built in.
I guess there's also the option of serializing the items into JSON when the submit button is clicked, and sending that across in the payload to be deserialized into strongly-typed objects. Thoughts on that appreciated also.
Thank you.
Aucun commentaire:
Enregistrer un commentaire