简体   繁体   中英

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM