I have two models, Unit and Building. In Unit, there is a BuildingId (a foreign key) field which ties to a Building object. I am writing a collection view to display all the Units. I also have a service method to get a building object based on its Id. I can display the BuildingId for each Unit in a label in the collection view. However, i don't know how to display the Building.Name instead.
I am not sure if i am looking at the right direction because there seems to be an easy way of doing this. I am currently looking into extending a label class, and do customization in there?
To display the Building.Name instead of BuildingId for each Unit in your collection view, you'll need to fetch the Building object using the BuildingId and then display the Building.Name.
Sample code:
public class Unit
{
public int Id { get; set; }
public int BuildingId { get; set; }
public string BuildingName { get; set; } // Add this property
// Other properties...
}
While fetch units:
List<Unit> units = // Your code to fetch all units
foreach (var unit in units)
{
var building = _buildingService.GetBuildingById(unit.BuildingId);
unit.BuildingName = building?.Name; // Set the building name
}
From Building service:
public Building GetBuildingById(int id)
{
// Your implementation to fetch a building by its id
return BuildingList.FirstOrDefault(a=>a.BuildingId == id);
}