ASP .Net 核心错误 CS1061 ICollection 错误

ASP .Net Core Error CS1061 ICollection Error

我正在从事一个项目,其中部分功能是将城市的郊区与其父城市相关联。比如我住在俄亥俄州的Grandview,它是俄亥俄州哥伦布市(州首府)的郊区。

我正在使用 ASP .Net Core 2.2 Razor Pages(非 MVC)、EF Core 和 SQL 服务器。

我的 IDE 是 VS 2019。

我有两个类:

 public class City
    { 

        [Key]
        public int Id { get; set; }

        [Required]
        [Display(Name = "City")]
        public string Description { get; set; }

        public int StateID { get; set; }
        [ForeignKey("StateID")]
        public virtual State State { get; set; }

        public int? ParentCityId { get; set; }
        [ForeignKey("ParentCityId")]
        public virtual ParentCity ParentCity { get; set; }

    }

城市是郊区(在我的例子中是正佳)

 public class ParentCity
    {

        public ParentCity()
        {
            this.Cities = new HashSet<City>();
        }

        [Key]
        public int Id { get; set; }

        [Required]
        public string Description { get; set; }

        public ICollection<City> Cities { get; set; }

    }

父城市是主要城市(在我的例子中是哥伦布)

这是我的 Index.cshtml 页面模型:

public class IndexModel : PageModel
    {
        [TempData]
        public string StatusMessage { get; set; }

        private readonly Data.ApplicationDbContext _context;

        public IndexModel(Data.ApplicationDbContext context)
        {
            _context = context;
        }

        public ICollection<ParentCity> ParentCity { get; set; }

        public async Task<IActionResult> OnGetAsync()
        {
            ParentCity = await _context.ParentCity
                                       .Include(c => c.Cities)
                                       .OrderBy(c => c.Description)
                                       .AsNoTracking()
                                       .ToListAsync();

            return Page();
        }
    }

This SQL selects all of the Parent Cities in the database table, and their associated City.

我在表中有以下数据:

Parent City:
Id = 1
Description = "Columbus"

City:
Id = 1 Grandview
ParentCityId = 1

Id = 2 Worthington
ParentCityId = 1

在我的 index.cshtml 中,我有以下代码:

 @foreach (var item in Model.ParentCity)
      {
       <tr>
           <td class="align-middle">
           @Html.DisplayFor(modelItem => item.Description)
           </td>
           <td class="align-middle">
           @Html.DisplayFor(modelItem => item.Cities.Description)
           </td>                            
       </tr>
       }

在线:

@Html.DisplayFor(modelItem => item.Cities.Description)

我收到以下错误:

CS1061 'ICollection<City>' does not contain a definition for 'Description' and no accessible extension method 'Description' accepting a first argument of type 'ICollection<City>' could be found (are you missing a using directive or an assembly reference?)

感谢任何帮助解释我收到此错误的原因。我已经看了一个小时了,无法弄清楚。 Google 还不是我的朋友。

谢谢。

错误在你的cshtml:

@Html.DisplayFor(modelItem => item.Cities.Description)

您正在尝试访问 CitiesDescription 属性 - 但 CitiesICollection

解决方法之一是更改 foreach 以遍历城市并打印每个城市的值:

@foreach (var item in Model.ParentCity.Cities)
{
    <tr>
        <td class="align-middle">
            @Html.DisplayFor(modelItem => item.ParentCity.Description)
        </td>
        <td class="align-middle">
            @Html.DisplayFor(modelItem => item.Description)
        </td>                            
     </tr>
}