title: display attribute from model by its primary key with ViewBag and asp-items in mvc asp.net application
body: <p>I'm building a simple application with ASP.NET MVC. I made two models (Class and Checklist) and Checklist class is supossed to have an m-1 relationship to Class class. I used EF Core to generate the database and the code of the controllers and the views. Specifically, I'm interested in the Create view of my Checklist model, since it has this foreign key that points to Class. The form includes a field as follows:</p>
  <pre><code>        &lt;div class=&quot;form-group&quot;&gt;
              &lt;label asp-for=&quot;ClassId&quot; class=&quot;control-label&quot;&gt;&lt;/label&gt;
              &lt;select asp-for=&quot;ClassId&quot; class =&quot;form-control&quot; asp-items=&quot;ViewBag.ClassId&quot;&gt;
                 
              &lt;/select&gt;
          &lt;/div&gt;
  </code></pre>
  <p>Being ClassId the foreign key. When I add an item in the Create Class form, now it displays just the PK of that item in this select element. I wanted to know if there's any way in which I can display the name of the Class object, not just its ID.</p>
  <p>I tried using ViewBag.Name in the asp-items tag helper but it doesn't work. Please let me know if I need to add some code within options tags, but if so, how can I access that information from my Controller. Here's a piece of code of my Checklist controller:</p>
  <pre><code>// GET: Checklists
      public async Task&lt;IActionResult&gt; Index()
      {
          var applicationDbContext = _context.Checklist.Include(c =&gt; c.Class);
          return View(await applicationDbContext.ToListAsync());
      }
  </code></pre>
  <p>Do I need to modify some code here as well in order to have the names of the Class items?</p>
  <p>Please notice that I just want to display this info for UI purposes, but when inserting the item, I want it to insert the row with the ID, since this is how the database and the models were built.</p>
  <p>Thank you!</p>