Hi moepyag,
There are different ways to retain the DropDownList value in ASP.Net Core. Its up to your program which suits best for particalar way.
1. Retain value using TempData object.
Retain DropDownList selected value after submitting Form in ASP.Net Core MVC
2. Retain value using Session.
Keep (Retain) DropDownList selection throughout application in ASP.Net Core MVC
3. I have implemented this using ViewData object and I find it very simple.
I did it with both Hard Code and Dynamically with Entity Framework.
Hard Code
Controller
public class HomeController : Controller
{
public IActionResult Index()
{
ViewBag.Options = new List<SelectListItem>
{
new SelectListItem { Value = "1", Text = "Mango" },
new SelectListItem { Value = "2", Text = "Orange },
new SelectListItem { Value = "3", Text = "Banana" }
};
return View();
}
[HttpPost]
public IActionResult Index(int selectedValue)
{
ViewBag.Options = new List<SelectListItem>
{
new SelectListItem { Value = "1", Text = "Mango" },
new SelectListItem { Value = "2", Text = "Orange" },
new SelectListItem { Value = "3", Text = "Banana" }
};
ViewBag.SelectedValue = selectedValue;
return View();
}
}
View
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@{
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 name="selectedValue">
<option value="">-- Please Select --</option>
@foreach (var option in ViewBag.Options)
{
<option value="@option.Value" selected="@(option.Value == ViewBag.SelectedOption?.ToString() ? "selected" : null)">
@option.Text
</option>
}
</select>
<input type="submit" value="Submit" />
</form>
</body>
</html>
Dynamically
Database
I am making use of a table named Fruits whose schema is defined as follows.

The Fruits table has the following records

Note: You can download the database table SQL by clicking the download link below.
Download SQL file
Model
public class FruitModel
{
[Key]
public int FruitId { get; set; }
public string FruitName { get; set; }
}
Database Context
For configuring Entity Framework in ASP.Net Core (.Net Core 7), please refer my article ASP.Net Core 7: Simple Entity Framework Tutorial with example. It covers all the information needed for connecting and configuring Entity Framework with ASP.Net Core.
using Microsoft.EntityFrameworkCore;
namespace Sample_385809
{
public class DBCtx : DbContext
{
public DBCtx(DbContextOptions<DBCtx> options) : base(options)
{
}
public DbSet<FruitModel> Fruits { get; set; }
}
}
Controller
public class HomeController : Controller
{
private DBCtx Context { get; }
public HomeController(DBCtx _context)
{
this.Context = _context;
}
public IActionResult Index()
{
ViewBag.Options = this.Context.Fruits.Select(f => new SelectListItem { Value = f.FruitId.ToString(), Text = f.FruitName });
return View();
}
[HttpPost]
public IActionResult Index(int selectedValue)
{
ViewBag.Options = this.Context.Fruits.Select(f => new SelectListItem { Value = f.FruitId.ToString(), Text = f.FruitName });
ViewBag.SelectedOption = selectedValue;
return View();
}
}
View
The HTML Markup of View is same as given above in Hard Code section.
Screenshot

Glad to assist!