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?

2
  • 1
    Your model needs to be IList, not IEnumerable Commented Sep 9, 2014 at 8:00
  • 1
    and remove the . between Model and [i] Commented Sep 9, 2014 at 8:02

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you exactly what I want "." between Model and [i] is a typo
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)
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.