2

I would like to have a table within a form, with an update button. Ideally each row would be a form, but that doesn't fit html spec.

my view looks like this

@model IEnumerable<DabTrial.Models.ManageFilesModel>

@foreach(var item in Model) {
<tr>
    <td>
        @Html.TextBoxFor(modelItem=>item.Name)
        ....

when I look at the markup, the inputs within each row for the same property have the same id

<input id="item_Name" type="text" value="ParentGuardianPMH.pdf" name="item.Name">
...
<input id="item_Name" type="text" value="ParentGuardianMMH.pdf" name="item.Name">

which is obviously invalid - how should I be doing this. Thank you

Brent
  • 4,611
  • 4
  • 38
  • 55
  • You can explicitly set the ID using one of the `TextBoxFor<>()` overloads (assuming everything else is the way you want it). You may want to also consider an editor template which accounts for one level of nested objects (it doesn't look like the name elements will match up with what the default model binder expects). – Tim M. Jun 06 '13 at 06:35

2 Answers2

4

you need such thing

@for(int i=0; i< Model.Count(); i++)
{
  @Html.TextBoxFor(item => item[i].Name)
}

read this post about model binding to a list http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx

xurca
  • 2,426
  • 3
  • 24
  • 29
2

use a for loop, not a foreach, and you'll have distinct id.

Raphaël Althaus
  • 59,727
  • 6
  • 96
  • 122