tags:

views:

32

answers:

2

I have many rows that I display on a page, and each row has a textbox with a user entered value in it.

When the user submits the form, I have take each row's textbox value, and do some db work on them.

The db work is performing updates, so I have to be able to reference the ID of the row also (which I have to store somewhere??)

what are my options here?

+2  A: 

You need to set the name of the input(s) correctly. The Action won't see which "row" is associated with the value, nor will it see the id. It will only see the name.

Craig Stuntz
It was my understanding that, in the absence of the name attribute, MVC would use the 'id' in order to set the object value of the model that's being bound. Is this not correct?
Dan Atkinson
Dan, that's wrong. You may be thinking of the fact that `Html.TextBox` and the like set *both* the `id` and the `name`. But MVC isn't even involved here. The *browser* will send the `name` and ignore the `id` (if any) when submitting a form. If there's no `name`, then the element won't be sent, period.
Craig Stuntz
Thanks for the clarification, Craig.
Dan Atkinson
+1  A: 

Whilst this method isn't exactly 'nice', it would work for what I believe you may want.

Dan Atkinson
I like this article, but it is out of date, so I wrote mine: http://caughtintoacameltrap.blogspot.com/2009/12/aspnet-binding-model-to-list.html
LukLed
Hey. I'm sorry, but I'm not sure what differences your article makes to the one by Phil Haack. They both show the same way of model binding. Also, I commented on your post with a nicer way of doing foreach loops that require an index - `Model.Select((x, i)=> new { Current = x, Index = i })`.
Dan Atkinson
@Dan Atkinson: Thanks for comment. My solution doesn't define hidden field with `products.Index` name. It is not needed anymore. Indexing from 0 is enough.
LukLed
Ah right. Fair enough then!
Dan Atkinson