Search code examples
asp.net-mvc-4editorfor

ASPNET MVC LabelFor or EditorFor using for loop


I am new to ASPNET MVC. I have searched many hours but couldn't find a way to solve my simple problem.

For a Model with properties like "property1" "property2" "property3", I want to simplify the code in a View to something like:

for (i = 1; i <=3; i++) {
    @Html.EditorFor(model=>model.["property"+i];
}

How to replace the expression with custom names?

Thanks a lot


Solution

  • You can use the weakly typed version :

    for (i = 1; i <=3; i++) {
        @Html.Editor("property"+i);
    }