title: How can I display the selectedValue (from database) in a dropdown-field from the Edit method in ASP.NET Core MVC
body: <p>In the following code of an ASP.NET Core 3.1 MVC project, the <code>Get</code> action method <code>Edit (...)</code> is displaying a dropdown list of zipcodes. It seems that by default, the dropdown displays the first zipcode in the list (i.e. 1301) as the selected value.</p>
  <p>If I want to display the selected value from database in the dropdown list, how can I achieve it from action method <code>Edit(...)</code> without creating a view model?</p>
  <p>I have a rather complex data model, where zipcodes and city is members in the table Shop, but that table has a many-to-many relationship with product by a conjunction table ProductShop. I'm a beginner so I have been trying to follow this tutorial, but it doesn't get me so far. <a href="https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/update-related-data?view=aspnetcore-5.0" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/update-related-data?view=aspnetcore-5.0</a>
  This is my <code>Edit</code> get method in the controller:</p>
  <pre><code>// GET: Products/Edit/5
  public async Task&lt;IActionResult&gt; Edit(int? id) {
          if (id == null) {
              return NotFound();
          }
  
          var product = await _context.Product
              .Include(p =&gt; p.Brand)
              .Include(p =&gt; p.Price)
              .Include(p =&gt; p.ProductCategory)
                  .ThenInclude(p =&gt; p.Category)
              .Include(p =&gt; p.ProductShop)
                  .ThenInclude(ps =&gt; ps.Shop)
              .Include(s =&gt; s.ProductShop)
                  .ThenInclude(ps =&gt; ps.Shop)
                      .ThenInclude(s =&gt; s.Zip)
              .FirstOrDefaultAsync(m =&gt; m.Id == id);
  
          if (product == null) {
              return NotFound();
          }
  
          ViewData[&quot;BrandId&quot;] = new SelectList(_context.Brand, &quot;Id&quot;, &quot;BrandName&quot;, product.BrandId);
  
      // this is the method where I want to display zipcodes with the help from viewbag
      ViewData[&quot;ZipId&quot;] = new SelectList(_context.Zipcode.ToList(), &quot;Id&quot;, &quot;Zip&quot;, product.ProductShop.FirstOrDefault().Shop.Zip.Id);
  
      PopulateSelectedCategories(product);
      return View(product);
  }
  </code></pre>
  <p>My dropdown list in the view:</p>
  <pre><code>@model Vegetarian_Products.Models.DB.Product
  @*@model Vegetarian_Products.ViewModels.ProductDetailsVM*@
  @{
      ViewData[&quot;Title&quot;] = &quot;Edit&quot;;
  }
  
  &lt;h1&gt;Edit&lt;/h1&gt;
  
  &lt;h4&gt;Product&lt;/h4&gt;
  &lt;hr /&gt;
  &lt;div class=&quot;row&quot;&gt;
      &lt;div class=&quot;col-md-4&quot;&gt;
          &lt;form asp-action=&quot;Edit&quot;&gt;
  //....
  
                  &lt;div class=&quot;form-group&quot;&gt;
                      &lt;label asp-for=&quot;ProductShop.FirstOrDefault().Shop.Zip.Zip&quot; class=&quot;control-label&quot;&gt;&lt;/label&gt;
                      &lt;select asp-for=&quot;ProductShop.FirstOrDefault().Shop.Zip.Zip&quot; class=&quot;form-control&quot; asp-items=&quot;ViewBag.ZipId&quot;&gt;  
                          &lt;/select&gt;
                      &lt;span asp-validation-for=&quot;ProductShop.FirstOrDefault().Shop.Zip.Zip&quot; class=&quot;text-danger&quot; /&gt;
                  &lt;/div&gt;
  
   &lt;div class=&quot;form-group&quot;&gt;
                      &lt;input type=&quot;submit&quot; value=&quot;Save&quot; class=&quot;btn btn-primary&quot; /&gt;
                  &lt;/div&gt;
              &lt;/form&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  
  &lt;div&gt;
      &lt;a class=&quot;btn btn-primary&quot; asp-controller=&quot;Products&quot; asp-action=&quot;Index&quot;&gt;Back to List&lt;/a&gt;
  &lt;/div&gt;
  
  @section Scripts {
      @{await Html.RenderPartialAsync(&quot;_ValidationScriptsPartial&quot;);}
  }
  
  </code></pre>
  <p>Model class for zipcodes:</p>
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.ComponentModel.DataAnnotations;
  
  namespace Vegetarian_Products.Models.DB {
      public partial class Zipcode {
          public Zipcode() {
              Shop = new HashSet&lt;Shop&gt;();
          }
  
          public int Id { get; set; }
  
          [Required(ErrorMessage = &quot;Tilføj postnr&quot;)]
          [DisplayName(&quot;Postnr&quot;)]
          public int Zip { get; set; }
          [DisplayName(&quot;By&quot;)]
          public string City { get; set; }
  
          public virtual ICollection&lt;Shop&gt; Shop { get; set; }
  
      }
  }
  </code></pre>
  <p>Screenshot of Edit View running, and there is no selected value from database:</p>
  <p><a href="https://i.stack.imgur.com/i55mJ.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/i55mJ.jpg" alt="EditView running - no selectedValue" /></a></p>
  <p>Model of table-relations:</p>
  <p><a href="https://i.stack.imgur.com/CB55j.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CB55j.jpg" alt="Table-relations" /></a></p>