Hi moepyag,
The way you are binding DropDownList using DataTable and DataRow in ASP.Net Web Forms is a more complicated and lengthy while implementing same in ASP.Net Core.
Below is the easy example to do it.
Refer below code:
Controller
public class HomeController : Controller
{
public IActionResult Index()
{
List<YearModel> years = new List<YearModel>();
for (int year = 2000; year <= DateTime.Now.Year; year++)
{
years.Add(new YearModel { Year = year.ToString() });
}
return View(new SelectList(years, "Year", "Year"));
}
}
public class YearModel
{
public string Year { get; set; }
}
View
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@model SelectList
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<form method="post" asp-controller="Home" asp-action="Index">
<select id="ddlFruits" name="Year" asp-items="Model">
<option value="0">Please select</option>
</select>
</form>
</body>
</html>
You can also bind it with data from the database using ADO.Net and Entity Framework.
Please refer below articles:
Bind (Populate) DropDownList using ADO.Net in ASP.Net Core
ASP.Net Core: DropDownList with Entity Framework Tutorial with example
Still if you want to do it the way you are doing in ASP.Net Web Forms, please let me know.