无法将带有[]的索引应用于类型为icollection的表达式MVC视图ASP.NET核心2.1 [英] Cannot apply index with [ ] to an expression of type icollection MVC view ASP.NET core 2.1

查看:149
本文介绍了无法将带有[]的索引应用于类型为icollection的表达式MVC视图ASP.NET核心2.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

无法将带有[]的索引应用于类型为Icollection的表达式mvc view asp.net core 2.1

公共类SalesHeader 
{
public SalesHeader()
{

}
public int SalesYear {get;组; }
public int BranchCode {get;组; }
public ICollection< SalesFooter> SalesFooters {get;组; }
}





我尝试过:



 @for(var i = 0; i< Model.SalesFooters.Count; ++ i)
{
< tr>
< td>
@ Html.EditorFor(f => f.SalesFooters [i] .ItemCode)
< / td>
< td>
@ Html.EditorFor(f => f.SalesFooters [i] .Quantity)
< / td>
< td>
@ Html.EditorFor(f => f.SalesFooters [i] .UnitPrice)
< / td>
< / tr>
}





它显示我的for循环编译错误

无法将带有[]的索引应用于类型为Icollection的表达式mvc视图asp.net core 2.1 



如何解决此错误?

解决方案

ICollection是.NET中所有Collection类的基本接口: ICollection接口(System.Collections)| Microsoft Docs [ ^ ]

这意味着IList是来自ICollection的 派生 ,而不是相反。 IList支持索引,但ICollection不支持。



实现IList的对象可以存储在ICollection变量中,但是你不能在它上面使用IList方法(或索引器)不将其转换为IList(可能会失败,因为并非每个ICollection对象都是IList)。


ICollection接口没有定义[]索引器。你不能像数组一样使用它。



然而,它确实支持枚举。在您的情况下,您可以使用foreach替换for循环并使用枚举器:

 @ foreach( var 页脚  Model.SalesFooters)
{
< tr>
< td>
@ Html.EditorFor(f = > footer.ItemCode)
< / td >
< td>
@ Html.EditorFor(f = > footer.Quantity)
< / td >
< td>
@ Html.EditorFor(f = > footer.UnitPrice)
< / td >
< / tr >
}


problem
cannot apply index with [ ] to an expression of type Icollection mvc view asp.net core 2.1

public class SalesHeader
    {
        public SalesHeader()
        {

        }
        public int SalesYear { get; set; }
        public int BranchCode { get; set; }
        public ICollection<SalesFooter> SalesFooters { get; set; }
    }



What I have tried:

@for (var i = 0; i < Model.SalesFooters.Count; ++i)
                   {
                       <tr>
                           <td>
                               @Html.EditorFor(f => f.SalesFooters[i].ItemCode)
                           </td>
                           <td>
                               @Html.EditorFor(f => f.SalesFooters[i].Quantity)
                           </td>
                           <td>
                               @Html.EditorFor(f => f.SalesFooters[i].UnitPrice)
                           </td>
                       </tr>
                   }



it show my in for loop above compile error

cannot apply index with [ ] to an expression of type Icollection mvc view asp.net core 2.1


How to solve this error please ?

解决方案

ICollection is the base interface for all Collection classes in .NET: ICollection Interface (System.Collections) | Microsoft Docs[^]
Which means that IList is derived from ICollection, not the other way around. IList supports indexing, but ICollection does not.

An object that implements IList can be stored in an ICollection variable, but you cannot use IList methods (or indexers) on it without casting it to a IList (which may fail, because not every ICollection object is an IList).


The ICollection interface doesn't have a [] indexer defined. You cannot use it like an array.

It does, however, support enumeration. In your case, you can just swap out the for loop with foreach and use the enumerator instead:

@foreach (var footer in Model.SalesFooters)
{
    <tr>
        <td>
            @Html.EditorFor(f => footer.ItemCode)
        </td>
        <td>
            @Html.EditorFor(f => footer.Quantity)
        </td>
        <td>
            @Html.EditorFor(f => footer.UnitPrice)
        </td>
    </tr>
}


这篇关于无法将带有[]的索引应用于类型为icollection的表达式MVC视图ASP.NET核心2.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆