title: How to populate list in ASP.NET Entity Framework from database
body: <p>I'm trying to make app where I can manage clients. All my clients has his own client that I manage it. So I made database with two tables <code>tboClienti</code> for my clients and <code>tboSottoClienti</code> for my "under clients". </p>
  
  <p>This is the structure of tables :</p>
  
  <p><a href="https://i.stack.imgur.com/8kd91.png" rel="nofollow noreferrer">Structure of table Clients - tboClienti</a> and <a href="https://i.stack.imgur.com/QLYwd.png" rel="nofollow noreferrer">structure of table Under Clients - tboSottoClienti</a>. </p>
  
  <p>I'm working on CRUD operations for my "under clients", and I wanted to make drop down list where I can select my client and insert info (name, surname, company, phone) for my under client.</p>
  
  <p><a href="https://i.stack.imgur.com/IA9tZ.png" rel="nofollow noreferrer">This is how I imagine it</a>.</p>
  
  <p>I made a controller model and view for my under client, but I don't know how to make list in my View Razor Page.</p>
  
  <p>This is controller :</p>
  
  <pre><code>public class SottoClientiController : Controller
  {
      private readonly AppDbContext _db;
  
      public SottoClientiController(AppDbContext db)
      {
          _db = db;
      }
  
      public IActionResult Index()
      {
          var datiSottoClienti = _db.tboSottoClienti.ToList();
          return View(datiSottoClienti);
      }
  
  
      public IActionResult CreareLista()
      {
          ViewData["lstClieti"] = new SelectList(_db.tboClienti, "Id", "Nome_azienda");
          return View();
      }
      public IActionResult CreareSottoCliente()
      {
          return View();
      }
  
      [HttpPost]
      public async Task&lt;IActionResult&gt; CreareSottoCliente(SottoCliente sottoCliente) 
      {
          if (ModelState.IsValid)
          {
              _db.Add(sottoCliente);
              await _db.SaveChangesAsync();
              return RedirectToAction("Index");
          }
  
          return View(sottoCliente);
      }
  }
  </code></pre>
  
  <p>This is the model class:</p>
  
  <pre><code>public class SottoCliente
  {
      [Key]
      public int Id { get; set; }
  
      [Required(ErrorMessage = "Inserisci il nome di proprietario dell'azienda")]
      [Display(Name = "Nome")]
      public string Nome { get; set; }
  
      [Required(ErrorMessage = "Inserisci il cognome di proprietario dell'azienda")]
      [Display(Name = "Cognome")]
      public string Cognome { get; set; }
  
      [Display(Name = "Azienda")]
      public string Azienda { get; set; }
  
      [Required(ErrorMessage = "Inserisci il numero di telefono dell'azienda")]
      [DataType(DataType.PhoneNumber)]
      [Display(Name = "Telefono")]
      [RegularExpression(@"^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$",
         ErrorMessage = "Numero non valido")]
      public string Cellulare { get; set; }
  }
  </code></pre>
  
  <p>I don't know how to get the data from the table <code>tboClienti</code> to insert it into a list. So in that way I can select the Client for my under client.</p>
  
  <p>And this is the view: </p>
  
  <pre><code>&lt;h4&gt;Under Client&lt;/h4&gt;
  &lt;hr /&gt;
  &lt;div class="row"&gt;
      &lt;div class="col-md-4"&gt;
          &lt;form asp-action="CreareSottoCliente"&gt;
              &lt;div asp-validation-summary="ModelOnly" class="text-danger"&gt;&lt;/div&gt;
  
             &lt;div class="form-group"&gt;
              &lt;label asp-for="Azienda" class="control-label"&gt;&lt;/label&gt;
              &lt;select asp-for="Azienda" class="form-control" asp-items="@ViewBag.lstClieti"&gt;&lt;/select&gt;
              &lt;span asp-validation-for="Azienda" class="text-danger"&gt;&lt;/span&gt;
          &lt;/div&gt;
  
              &lt;div class="form-group"&gt;
                  &lt;label asp-for="Id" class="control-label"&gt;&lt;/label&gt;
                  &lt;input asp-for="Id" class="form-control" /&gt;
                  &lt;span asp-validation-for="Id" class="text-danger"&gt;&lt;/span&gt;
              &lt;/div&gt;
              &lt;div class="form-group"&gt;
                  &lt;label asp-for="Nome" class="control-label"&gt;&lt;/label&gt;
                  &lt;input asp-for="Nome" class="form-control" /&gt;
                  &lt;span asp-validation-for="Nome" class="text-danger"&gt;&lt;/span&gt;
              &lt;/div&gt;
              &lt;div class="form-group"&gt;
                  &lt;label asp-for="Cognome" class="control-label"&gt;&lt;/label&gt;
                  &lt;input asp-for="Cognome" class="form-control" /&gt;
                  &lt;span asp-validation-for="Cognome" class="text-danger"&gt;&lt;/span&gt;
              &lt;/div&gt;
              &lt;div class="form-group"&gt;
                  &lt;label asp-for="Azienda" class="control-label"&gt;&lt;/label&gt;
                  &lt;input asp-for="Azienda" class="form-control" /&gt;
                  &lt;span asp-validation-for="Azienda" class="text-danger"&gt;&lt;/span&gt;
              &lt;/div&gt;
              &lt;div class="form-group"&gt;
                  &lt;label asp-for="Cellulare" class="control-label"&gt;&lt;/label&gt;
                  &lt;input asp-for="Cellulare" class="form-control" /&gt;
                  &lt;span asp-validation-for="Cellulare" class="text-danger"&gt;&lt;/span&gt;
              &lt;/div&gt;
              &lt;div class="form-group"&gt;
                  &lt;input type="submit" value="Create" class="btn btn-primary" /&gt;
              &lt;/div&gt;
          &lt;/form&gt;
      &lt;/div&gt;
  &lt;/div&gt; 
  </code></pre>
  
  <p>I insert my connection string into <code>appsettings.json</code>, and I add it to <code>ConfigureServices();</code>.</p>
  
  <p>And this is the class for the database context :</p>
  
  <pre><code> public class AppDbContext: DbContext
   {
          public AppDbContext(DbContextOptions&lt;AppDbContext&gt; options): base (options)
          {
          }
  
          public DbSet&lt;Tecnico&gt; tboTecnici { get; set; }
          public DbSet&lt;Cliente&gt; tboClienti { get; set; }
          public DbSet&lt;SottoCliente&gt; tboSottoClienti { get; set; }
  }
  </code></pre>
  
  <p>Any suggestion how to get data from database and insert it to list?</p>
  
  <p>Thanks in advance!</p>