ASP .Net核心错误CS1061 ICollection错误

问题描述 投票:-1回答:1

我正在一个项目中,部分功能是将城市的郊区与其父城市联系起来。例如,我住在俄亥俄州格兰德维尤(Grandview),这是俄亥俄州哥伦布(州首府)的郊区。

我使用ASP.Net Core 2.2剃刀页面(非MVC),EF Core和SQL Server。

我的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; }

    }

城市是郊区(在我的示例中为Grandview)

 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>'不包含针对“说明”,并且没有可访问的扩展方法“说明”可以找到接受类型为'ICollection<City>'的第一个参数(您是否缺少using指令或程序集引用?)

任何帮助您解释我为什么会收到此错误的帮助。我已经看了一个小时了,无法弄清楚。 Google不是我的朋友。

谢谢。

c# asp.net-core razor html-helper ef-core-2.2
1个回答
0
投票

错误出在您的cshtml

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

您正在尝试访问DescriptionCities属性-但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>
}
© www.soinside.com 2019 - 2024. All rights reserved.