MVC EditforTemplate: Index rendered Wrong

92 views Asked by At

In the EditorTemplates, I have a Template which accepts and Model of Type List<string>. This should create an Textbox for each string in the list.

In the Model, the Property has the [UIHint("EditList")]. Now when I render it to the Page, the Template is called correctly, but the index is set wrong. When I submit the form I get:

MyList.[0]:test123

Instead of

MyList[0]:test123

I'm using MVC 3!, the same code workd in my test project which uses MVC 5

View:

<div class="col-md-10">
    @Html.EditorFor(model => model.MyList)
    @Html.ValidationMessageFor(model => model.MyList)
</div>

Model:

public class FormTest
    {
        [UIHint("EditListWithAddButton")]
        public List<string> MyList { get; set; }
    }

EditorForTemplate:

@model List<string>
<div class="EditListWithAddButton">
    <ul>
        @for (int i = 0; i < Model.Count(); i++)
        {
            <li>@Html.EditorFor(model => Model[i])</li>
        }
    </ul>
</div>
1

There are 1 answers

0
Stefan On

Brute force solution but, don't have the time to debug it any futher...

@Html.Raw(@Html.EditorFor(model => Model[i])
.ToString().Replace("__", "_").Replace(".[", "["))

this replaces the Editfortempalt in away, that the index is correct again...

If anybody has a better solution / can explain the problem please tell me