1

I want to edit multiple (say 4) instance of my model Restriction:

public int RestrictionID { get; set; }
public string portefeuille { get; set; }

I try to do this in my view:

@model IEnumerable<Management.Models.Restriction>
@for ( int i= 0; i < 4; i++)
{
    @Html.EditorFor(_ => Model.[i].portefeuille)
}

But I have an error that I can't use indexation on type IEnumerable.

Can somebody help me to solve this problem?

Yuliam Chandra
  • 14,494
  • 12
  • 52
  • 67
intern
  • 225
  • 1
  • 3
  • 14

2 Answers2

1

try below code using ILIst because in IEnumerable we cant use indexation ..for using indexation you should go for IList + remove extra "." between Model and [i] :-

 @model ILIst <Management.Models.Restriction>
 @for ( int i= 0; i < 4; i++)
{
   @Html.EditorFor(_ => Model[i].portefeuille)
 }

For more information have a look here :-

Cannot apply indexing with [] to an expression of type 'System.Collections.Generic.IEnumerable<>

Community
  • 1
  • 1
Neel
  • 11,625
  • 3
  • 43
  • 61
1

You could also use linq instead of indexing:

for(int i = 0; i < 4; i++)
{
    @Html.EditorFor(m => m.Skip(i).Take(1).First().portefeuille)
}
Dirk Trilsbeek
  • 5,873
  • 2
  • 25
  • 23