Error: Doesn't contain a public definition of GetEnumerator
Error: Doesn't contain a public definition of GetEnumerator
当我尝试 运行 应用程序时,我不断收到此错误,即
"CS1579: foreach statement cannot operate on variables of type 'Models.FloorPlanViewModel' because 'Models.FloorPlanViewModel' does not contain a public definition for 'GetEnumerator'"
它在 SummaryTable.cshtml
的 foreach 处崩溃
Line 34: </tr>
Line 35:
Line 36: @foreach (var item in Model)
Line 37: {
Line 38: <tr>
下面是 FloorPlanViewModel
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using USTGlobal.WorkBench.Repository;
namespace UI.Models
{
public class FloorPlanViewModel
{
public IEnumerable<SummaryConfiguration> sumConfig { get; set; }
}
}
有什么帮助吗?
@foreach (var item in Model.sumConfig)
只是为了添加更详细的解释。
我们什么时候可以使用foreach?
foreach 语句用于遍历实现 System.Collections.IEnumerable 或 System.Collections.Generic.IEnumerable 接口的对象集合。
foreach, in (C# Reference)
为什么你的代码不起作用?
@foreach (var item in Model)
您想要遍历 Model,但 它没有实现 IEnumerable。
为什么接受答案的代码有效?
@foreach (var item in Model.sumConfig)
因为Model.sumConfig 的类型是IEnumerable.
当我尝试 运行 应用程序时,我不断收到此错误,即
"CS1579: foreach statement cannot operate on variables of type 'Models.FloorPlanViewModel' because 'Models.FloorPlanViewModel' does not contain a public definition for 'GetEnumerator'"
它在 SummaryTable.cshtml
的 foreach 处崩溃Line 34: </tr>
Line 35:
Line 36: @foreach (var item in Model)
Line 37: {
Line 38: <tr>
下面是 FloorPlanViewModel
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using USTGlobal.WorkBench.Repository;
namespace UI.Models
{
public class FloorPlanViewModel
{
public IEnumerable<SummaryConfiguration> sumConfig { get; set; }
}
}
有什么帮助吗?
@foreach (var item in Model.sumConfig)
只是为了添加更详细的解释。
我们什么时候可以使用foreach?
foreach 语句用于遍历实现 System.Collections.IEnumerable 或 System.Collections.Generic.IEnumerable 接口的对象集合。
foreach, in (C# Reference)
为什么你的代码不起作用?
@foreach (var item in Model)
您想要遍历 Model,但 它没有实现 IEnumerable。
为什么接受答案的代码有效?
@foreach (var item in Model.sumConfig)
因为Model.sumConfig 的类型是IEnumerable.