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

大兔子大兔子 提交于 2019-12-04 04:27:12

问题


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

回答1:


ICollections are not ordered, so that cannot be indexed.

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




回答2:


Use IList

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


来源:https://stackoverflow.com/questions/7870093/cannot-apply-indexing-with-to-an-expression-of-type-icollection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!