Skip to content

Instantly share code, notes, and snippets.

@A-Programmer
Created December 18, 2017 07:50
Show Gist options
  • Save A-Programmer/2a7211cfc5667726fbc344e700046ca0 to your computer and use it in GitHub Desktop.
Save A-Programmer/2a7211cfc5667726fbc344e700046ca0 to your computer and use it in GitHub Desktop.
How to set drop down list items in ASP.NET Core from database
In action method of HomeController.cs :
[HttpGet]
public ActionResult TestAction()
{
ViewBag.Categories = db.Categories.ToList();
return View();
}
in View (TestAction.cshtml):
<select asp-for="Menu.Id" asp-items="@(new SelectList(@ViewBag.Categories,"Id","Title"))">
<option disabled selected>Choose Category</option>
</select>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment