<TargetFramework>netcoreapp3.1</TargetFramework>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.3" />
I have a view that produces a list of unique towns in alphabetical order.
Model:
public class dgTown
{
[Key]
public string town { get; set; }
}
View Model:
public List<SelectList> dgTowns { get; set; }
public string TownSelection { get; set; }
Database context:
public DbSet<dgTown> vtown { get; set; }
Controller:
var townQuery = _context.vtown.Select(t => t.town);
How do I convert the resulting EntityQueryable to a SelectList so the drop down can use it?
<select asp-for="TownSelection" asp-items="Model.dgTowns">
<option value="">All</option>
</select>
Please read the microsoft documentation about dropdown.
Example:
View Model:
Controller:
View: