Processing variable number of form fields

Processing variable number of form fields

I am working on a form which displays information about orders. Each order has a unique id, but they are not necessarily sequential on the form. Also, the number of fields can vary (one field per row on the form). The input into the form will not be mapped straight into the database, but will be added to the current value in the database, and then saved. An example of the form is in the picture below - the callout on the right shows the id for each row. Form example

I know how to generate the form like this, but I can't work out how I can easily process each of these rows reliably. I also know how to give each of the fields a unique identifier, like name="order-23" or name="order[23]", but how can I translate that name so that I can update the related record in the database?

EDIT: One solution I can think of would be to iterate through every form field in the FormCollection, and if the name of the field matches the pattern, then I will extract the number from that field-name and process it.
However, I feel that there must be a much easier way to go about it - this method would likely involve a fair bit of string processing on each field, and there would possibly fall over if I have to add extra fields for each row later on.

OutOfMemoryException C# When Uploading Image Files


Using HttpContext.Current.Application to store simple data
Don't you have a list of IDs after postback? I believe you should not depend on what IDs are actually sent from the form, as anybody could change the IDs on the form to whatever they want, so it's a security issue. Cant open Nerd Dinner 1.0 VS 2008 SP1 MVC 2So you should after postback have a list of IDs you want to update (the same list you used to create the form with). After using Automapper to map a ViewModel how and what should I test?In this case, you know exactly what id string you should use to retrieve the value from FormCollection.. How to map table records to Any type using Castle ActiveRecord In case you really can't receive the list of IDs you are going to update, just use the FormCollection iteration as you suggested in your comment. Why do I get a TypeInitializationException when calling HtmlHelper.AntiForgeryToken?The string processing is not this expensive in comparation with all another stuff being done at request processing.. How to access model in jquery
Is it possible for a Master page to access Controller properties?


If you have the names, then simply read the values by using Request.Form["order-23"] or re-create the controls in page pre-init and you'll have access to the values in your save event directly through the created controls..


I've done this loads in my CMS.. Essentially i sort of cheated. . The idea is any thing like this ..... render the form to the client, then look at the source code gneerated. You should see this it generated a form tag with an action attrialthough e.. When you click the submit although ton the form will be sent to this url so in the case of an rule submission you would post the page back to OrderPage.aspx?OrderId=xxxx. then on the server you would build an update statement for your db this contained any thing like .... "Update orders where rule id =" + request.querystring["OrderId"]. So how did you update the action .... In the calling page lets say you have a link called "New Order", when this link is clicked you need to did 2 things ....
  1. redirect to this page.
  2. generate an rule id for this new order.
ok the first is simple, simply link it to this page.. the second .... if this page is not a postback if(!IsPostback) { /* receive a new id */ } depending on your rule id's this may involve generating a new guid or doing any thing like receive ting the next number in a list by doing a select max(id) from a db.. once you have this new id you should still be in the page_load event.. this.form.Action = this.form.Action + "?OrderId=" + yourNewOrderId;. ... tada .... Send page back to the client.. view the source..

88 out of 100 based on 43 user ratings 798 reviews