0

I have a little snippet of code here:

View:

@foreach (var lines in Model.KVpairs)
{
    @Html.TextBoxFor( m => lines, new { placeholder = lines })
}

In my application, I load the view containing this code twice, the first time, it works as intended, the second time, the values for all of the textboxes are the same value, but the placeholders are all different. Or in other words, the placeholders are working as intended, but the value(the actual text inside the box) is not.

Am I missing something very obvious?

EDIT:

wanted to add some pics:

before I sumbit the form, ive entered these values before I sumbit the form, ive entered these values

Here you can see the values of the KVpairs Here you can see the values of the KVpairs

And this is the result I get after form submission And this is the result I get after form submission

And these are the placeholders after the submission And these are the placeholders after the submission

Android
  • 1,230
  • 12
  • 20
  • Can provide server side codes ? – Mohsen Esmailpour Jul 09 '14 at 19:54
  • I don't believe the server side code will add much to the question, I am returning a partial view which contains the foreach loop and passing it a model, which contains KVpairs. I believe the issue lies in the htmlhelper, if it means anything this view is loaded using Ajax. – Android Jul 09 '14 at 20:04

1 Answers1

1

Instead of foreach loop try using for loop like this:

@for (var i = 0; i < Model.KVpairs.Count(); i++)
{
    @Html.TextBoxFor( m => Model.KVpairs[i], new { placeholder = lines })
}
Marko
  • 12,543
  • 10
  • 48
  • 58
  • I guess I should have mentioned that KVpairs was a List. Does this change your answer in any way? – Android Jul 09 '14 at 20:15
  • 1
    Thank you for your help, I even found a more extensive answer here: http://stackoverflow.com/questions/8894442/mvc-razor-view-nested-foreachs-model – Android Jul 09 '14 at 20:26
  • It does not change my answer and the reason why it has to be done this way is because of the ID and NAME tags that are assigned to elements rendered by the helper. In order for those to bind to your model properly you have to use the for loop with the index. Anyway the answer you linked points that out... – Marko Jul 09 '14 at 23:52
  • 1
    In order to return the number of elements in a list, you must call Count(), not Length. – Android Jul 10 '14 at 13:33