I am working on a project where part of the functionality is to associate the suburbs of cities with their parent city. For example, I live in Grandview, Ohio, which is a suburb of Columbus, Ohio (the state capital).
I a using ASP .Net Core 2.2 Razor Pages (non-MVC), EF Core, and SQL Server.
My IDE is VS 2019.
I have two classes:
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; }
}
City is the suburb (Grandview in my example)
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; }
}
Parent City is the major city (Columbus, in my example)
Here is my Index.cshtml Page Model:
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.
I have the following data in the tables:
Parent City:
Id = 1
Description = "Columbus"
City:
Id = 1 Grandview
ParentCityId = 1
Id = 2 Worthington
ParentCityId = 1
In my index.cshtml I have the following code:
@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>
}
On the line:
@Html.DisplayFor(modelItem => item.Cities.Description)
I am getting the following error:
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?)
Any help appreciated in explaining why I am getting this error. I have been looking at it for hour, and cannot figure out. Google has not been my friend.
Thanks.
The error is in your cshtml
:
@Html.DisplayFor(modelItem => item.Cities.Description)
You are trying to access the Description
property of Cities
- but Cities
is an ICollection
.
One was to fix it is to change your foreach
to loop over the cities and print the values for each city:
@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>
}