有人可以提供代码来修复这个错误吗?

“不能将带有 [] 的索引应用于类型为 'ICollection' 的表达式

本质上,我试图从一组对象中保存/绑定(bind)一个值。

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

最佳答案

ICollection s 没有排序,所以不能被索引。

相反,您应该使用具有 IList<T> 属性的单独 ViewModel 类。

关于asp.net-mvc-3 - 无法将 [] 索引应用于 ICollection 类型的表达式,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7870093/

10-12 12:23
查看更多