Search code examples
asp.net-mvc-4razorhtml-helperhtml.textboxfor

MVC razor behaving unexpectedly, displaying incorrect textbox values


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


Solution

  • 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 })
    }