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.
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", 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
Cant open Nerd Dinner 1.0 VS 2008 SP1 MVC 2
So 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 that 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 get 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 that expensive in comparation with all other stuff being done at request processing..
How to access model in jquery
Is it possible for a Master page to access Controller properties?
. The idea is something like this ..... render the form to the client, then look at the source code gneerated.
You should see that it generated a form tag with an action attribute.. When you click the submit button the form will be sent to that url so in the case of an order 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 that contained something like .... "Update orders where order id =" + request.querystring["OrderId"]. So how do you update the action .... In the calling page lets say you have a link called "New Order", when that link is clicked you need to do 2 things ....
- redirect to this page.
- generate an order id for this new order.
tada .... Send page back to the client.. view the source..