MVC ASP.net 定义 GetEnumerator 错误



所以我在标题中收到错误消息,我不明白为什么。

这是我如何在视图中引用模型的方式

@model IEnumerable<Lowflix.Models.LendingIndexModel>

这个 foreach 中出现的错误

@foreach (var item in Model)

真的无法向自己解释我是如何得到这个错误的,因为我什至声明了 IEnumerable。它包含多个对象。

这是错误消息:

foreach statemetn cannot operate on variables
of type "Models" because "Models" does not 
contain  a public instance of GetEnumerator

- 型:

using Lowflix.Core.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Lowflix.Models
{
public class LendingIndexModel
{
public LendingIndexModel()
{
}
public Guid LendingId { get; set; }
public Guid CustomerId { get; set; }
public Guid CopyId { get; set; }
public String Title { get; set; }

public DateTime StartDate { get; set; }
public DateTime? ReturnDate { get; set; }

public virtual Movie Movie { get; set; }
public virtual Customer Customer { get; set; }
public virtual Copy Copy { get; set; }
}

调试模型: 型号类型:

+       Model   {System.Linq.Enumerable.WhereSelectListIterator
<Lowflix.Core.Entities.Lending,
Lowflix.Models.LendingIndexModel>} System.Collections.Generic.IEnumerable
<Lowflix.Models.LendingIndexModel> 
{System.Linq.Enumerable.WhereSelectListIterator
<Lowflix.Core.Entities.Lending, 
Lowflix.Models.LendingIndexModel>}

请尝试使用以下代码。

@model List<Lowflix.Models.LendingIndexModel>

在"视图"页中,将"IEnumerable"更改为"列表"。

最新更新