0

How the @Html.EditorForModel(Model.ElementAt(i)) really works, if "model.count()" is 5. Why do I get a dimension of "5*5" elements?

@model List<model>
@for (int i = 0; i < Model.Count();i++ )
{                
    @Html.EditorForModel(Model.ElementAt(i))                
}

The only attempt that actually gave me 5 elements is:

@model List<model>

@Html.EditorForModel(Model)                

In the template for model I am using Javascript to change a value of the model.Numbers, using a "GetElementById("NumbersX")":

 @Html.TextBoxFor(m => m.Numbers, new { id = "NumbersX" })

Does anyone know a better way of implementing, or an already implemented function, to perform this task ?

James C
  • 901
  • 1
  • 18
  • 38

1 Answers1

1

EditorForModel outputs an editor for the whole model.

Your model has five elements, so one "whole editor" is five elements.
You call it five times, so you get 5x5 elements.

You probably meant to call EditorFor, not EditorForModel.

@Html.EditorFor(m => m.ElementAt(i)) 

But that will not work as you expect either. However, because you have a List as your model, you can make it work properly:

@Html.EditorFor(m => m[i]) 

The overload you are calling accepts Object additionalViewData as its parameter. You pass a lambda as if you were calling EditorFor, and that lambda simply does nothing.

Community
  • 1
  • 1
GSerg
  • 76,472
  • 17
  • 159
  • 346