当我试图运行应用程序时,我不断地收到这个错误,即
"CS1579: foreach statement cannot operate on variables of type 'Models.FloorPlanViewModel' because 'Models.FloorPlanViewModel' does not contain a public definition for 'GetEnumerator'"
它对SummaryTable.cshtml的冲击
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; }
}
}
有什么帮助吗?
发布于 2017-08-01 01:38:26
@foreach (var item in Model.sumConfig)
发布于 2017-08-28 16:07:06
只是为了增加一个更详细的解释。
什么时候我们可以使用foreach?
foreach语句用于迭代实现System.Collections.IEnumerable或System.Collections.Generic.IEnumerable接口的对象集合。
为什么您的代码不能工作?
@foreach (var item in Model)
您希望在模型中迭代,但是它没有实现IEnumerable.
为什么被接受的答案的代码工作?
@foreach (var item in Model.sumConfig)
因为Model.sumConfig 的类型是IEnumerable。
https://stackoverflow.com/questions/45427647
复制相似问题