I have figured out how to make a dropdownlist with static data, but I want to display data from my database. How do I fill my list with data from the model Neighbourhoods? I want to display all the NeighbourhoodNames in my dropdown.
I am using .net core 5.0 with MVC
My cshtml code:
<select asp-for="Neighbourhood" asp-items="Model.NeighbourhoodList"></select>
My Model:
using Microsoft.AspNetCore.Mvc.Rendering;
using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
namespace MyProject.Models
{
[Keyless]
public partial class Neighbourhoods
{
public string NeighbourhoodGroup { get; set; }
public string NeighbourhoodName { get; set; }
public List<SelectListItem> NeighbourhoodList { get; } = new List<SelectListItem>
{
new SelectListItem { Value = "a", Text = "New York" },
new SelectListItem { Value = "b", Text = "Los Angeles" },
};
}
}
My Controller:
// GET: Neighbourhoods/Create
public IActionResult Create()
{
var vm = new Neighbourhoods();
return View(vm);
}