Developer technologies | ASP.NET | ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Use ViewData to pass an object from a controller to a view in MVC Core. The following links illustrate how to configure the select tag helper.
https://code-maze.com/select-tag-helper-aspnetcore/
https://www.learnrazorpages.com/razor-pages/tag-helpers/select-tag-helper
Example
public IActionResult Index()
{
var options = new List<SelectListItem>() {
new SelectListItem()
{
Text = "Item 1",
Value = "1"
},
new SelectListItem()
{
Text = "Item 2",
Value = "3"
}
};
ViewData["UnitId"] = options;
return View();
}
View
@model MvcDemo.Models.SelectModel
@{
ViewData["Title"] = "Index";
}
<h1>Index</h1>
<form>
<select asp-for="UnitId" class="form-contorl" asp-items="@((List<SelectListItem>)ViewData["UnitId"])">
<option value="">--Select--</option>
</select>
</form>
I think there is a bug in your code that populates the select options since the ViewBag is null. If you share your code then we can help you figure that out as well.