title: MVC Core: Display another property based on primary key
body: <p>I have a model with a foreign key of another table.  In this case, the waiver object is related to an office object</p>
  
  <pre><code>public class Waiver
      {
          public int WaiverID { get; set; }
          [Required(ErrorMessage = "Please select an Office")]
          [Range(1, int.MaxValue, ErrorMessage = "Please select an Office")]
          public int OfficeID { get; set; }
          //...other properties
  }
  </code></pre>
  
  <p>I have a search result that filters by the OfficeID:</p>
  
  <pre><code> IEnumerable&lt;Waiver&gt; waiverList = repository.Waivers
                  .Where(wl =&gt; waiverNum == 0 || wl.WaiverID == waiverNum)
                  .Where(wl =&gt; officeId == 0 || wl.OfficeID == officeId)
  //...other search criteria
  </code></pre>
  
  <p>This is displayed in my View by:</p>
  
  <pre><code>@model IEnumerable&lt;Waiver&gt;
  
  @foreach (var item in Model)
      {
          &lt;tr&gt;
              &lt;td class="text-right"&gt;@item.WaiverID&lt;/td&gt;
              &lt;td class="text-right"&gt;@item.OfficeID&gt;&lt;/td&gt;
  @* other display columns *@
  </code></pre>
  
  <p>While the Waiver object passed to the View has the OfficeID property, I would like to display the Name property in the Office model as it is more useful to the user.  How can I accomplish this?</p>
  
  <p>My EF Repository is set up as:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Threading.Tasks;
  
  namespace Waiver.Models
  
  //This class implements the IWaiverRepository and gets its data using Entity Framework Core
  {
      public class EFWaiverRepository : IWaiverRepository
      {
  
  
          private ApplicationDbContext context;
  
          public EFWaiverRepository(ApplicationDbContext ctx)
          {
              context = ctx;
          }
  
  
          public IEnumerable&lt;Waiver&gt; Waivers =&gt; context.Waivers;
          public IEnumerable&lt;Office&gt; Offices =&gt; context.Offices;
  
          //*****Waiver Methods*****
          public void SaveWaiver(Waiver waiver)
          {
              //if the waiver number is 0, create a new waiver
              if (waiver.WaiverID == 0)
              {
                  context.Waivers.Add(waiver);
              }
              //if there wavier number exists, save the changes to the database
              else {
                  Waiver dbEntry = context.Waivers.FirstOrDefault(w =&gt; w.WaiverID == waiver.WaiverID);
                  if (dbEntry != null)
                  {
                      dbEntry.Requestor = waiver.Requestor;
                      dbEntry.RequestorEmail = waiver.RequestorEmail;
                      dbEntry.OfficeID = waiver.OfficeID;
                      dbEntry.RequestDate = waiver.RequestDate;
                      dbEntry.System = waiver.System;
                      dbEntry.Source = waiver.Source;
                      dbEntry.Requirement = waiver.Requirement;
                      dbEntry.MitigationPlan = waiver.MitigationPlan;
                      dbEntry.FinalAssessment = waiver.FinalAssessment;
                      dbEntry.Status = waiver.Status;
                      //get the signatures
                      dbEntry.PmSignature = waiver.PmSignature;
                      dbEntry.PmSignDate = waiver.PmSignDate;
                  }
              }
              context.SaveChanges();
          }
  
          public Waiver DeleteWaiver(int waiverID)
          {
              Waiver dbEntry = context.Waivers
                  .FirstOrDefault(w =&gt; w.WaiverID == waiverID);
              if (dbEntry != null)
              {
                  context.Waivers.Remove(dbEntry);
                  context.SaveChanges();
              }
              return dbEntry;
          }
  
          //*****Office Methods*****
          public void SaveOffice(Office office)
          {
              //if the ID=0, its a new entry. Add to db
              if (office.OfficeID == 0)
              {
                  context.Offices.Add(office);
              } else
              {
                  Office dbEntry = context.Offices.FirstOrDefault(o =&gt; o.OfficeID == office.OfficeID);
                  if (dbEntry != null)
                  {
                      dbEntry.Name = office.Name;
                      dbEntry.SiteID = office.SiteID;
                  }
              }
              context.SaveChanges();
          }
  
          public Office DeleteOffice(int officeID)
          {
              Office dbEntry = context.Offices
                  .FirstOrDefault(o =&gt; o.OfficeID == officeID);
              if (dbEntry != null)
              {
                  context.Offices.Remove(dbEntry);
                  context.SaveChanges();
              }
              return dbEntry;
          }
  </code></pre>
  
  <p>where the EFRepository implements:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Threading.Tasks;
  
  namespace Waiver.Models
  {
      public interface IWaiverRepository
      {
          IEnumerable&lt;Waiver&gt; Waivers { get; }
          IEnumerable&lt;Office&gt; Offices { get; }
          IEnumerable&lt;Site&gt; Sites { get; } 
  
          void SaveWaiver(Waiver waiver );
          Waiver DeleteWaiver(int waiverID);
  
          void SaveOffice(Office office);
          Office DeleteOffice(int officeID);
      }
  }
  </code></pre>