<select asp-for="Country.ContinentID" asp-items="Model.Continents"></select>
The asp-items attribute in a <select> element generates <option> tags according to the model property specified. It works in conjunction with the asp-for attribute to display the matching option and set the underlying value on a change.
using Microsoft.AspNetCore.Mvc.Rendering;public SelectList Continents { get; set; }public async Task<IActionResult> OnGetAsync(string id){Continents = new SelectList(_context.Continents, nameof(Continent.ID), nameof(Continent.Name));}
The SelectList type is declared in the page model code and assigned to a new SelectList() where each record in Continents grabs the ID as the <option> value and Name as the <option> display text.
The included <select> in the example would render as this HTML:
<select class="valid" id="Country_ContinentID" name="Country.ContinentID" aria-invalid="false"><option value="NA">North America</option><option value="SA">South America</option><option value="EU">Europe</option><!-- etc --></select>
The Entity Framework DbSet entities can manage complex queries using C# LINQ syntax. This is referenced from the System.Linq library.
All of the Where() and OrderBy() clauses are evaluated in the final statement that calls ToListAsync(). EF evaluates all options and generates a SQL SELECT statement with corresponding WHERE and ORDERBY clauses.
using System.Linq;var countries = from c in _context.Countriesselect c;countries = countries.Where(c => c.Name.Contains("Russia"));countries = countries.Where(c => c.ContinentID == 3);countries = countries.OrderBy(c => c.Name);List<Country> Countries = await countries.ToListAsync();