intern
intern

Reputation: 225

Edit multiple instances of models asp.net mvc

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?

Upvotes: 1

Views: 185

Answers (2)

Neel
Neel

Reputation: 11741

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<>

Upvotes: 1

Dirk Trilsbeek
Dirk Trilsbeek

Reputation: 6033

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

Upvotes: 1

Related Questions