This code for select multiple with default selected options was working fine on .netcore 5 but after upgrade to .net 8 selected options not working
c# code
List<int> currentContentCatIDs = itemLoc.Content.ContentCategories.Where(x => x.Category.CatTypeId == 2).Select(a => a.CatId).ToList();
if (currentContentCatIDs != null && currentContentCatIDs.Count > 0)
{
List<SelectListItem> catsSelectList = new();
foreach (var instructorLoc in instructors)
catsSelectList.Add(new SelectListItem { Text = instructorLoc.Title, Value = instructorLoc.CatId.ToString(), Selected = currentContentCatIDs.Contains(instructorLoc.CatId) });
ViewBag.MultiInstructors = catsSelectList;
}
else
ViewBag.MultiInstructors = new SelectList(instructors, "CatId", "Title", "");
view :
<select asp-for="InstructorsCatIDs" asp-items="ViewBag.MultiInstructors" class="select2 custom-select block" multiple="multiple"></select>
viewmodel :
public int[] InstructorsCatIDs { get; set; }