Cannot apply indexing with [] to an expression of

2020-04-10 03:18发布

Can someone please provide code to fix this error?

"Cannot apply indexing with [] to an expression of type 'ICollection'

Essentially, I'm trying to save/bind a value from a collection of objects.

@model MVC3.Models.Parent

@Html.EditorFor(model => model.Bs[0].Val) 

public  class A
{
    public int Name { get; set; }
    public virtual ICollection<B> Bs { get; set; }
}

public  class B
{
    public int Val { get; set; }
    public virtual A A { get; set; }
}

2条回答
迷人小祖宗
2楼-- · 2020-04-10 03:37

ICollections are not ordered, so that cannot be indexed.

Instead, you should use a separate ViewModel class with IList<T> property.

查看更多
聊天终结者
3楼-- · 2020-04-10 03:44

Use IList

public  class A
{
   public int Name { get; set; }
   public virtual IList<B> Bs { get; set; }
}
查看更多
登录 后发表回答