User-29703693 posted
I'm generating a select list using:
<select asp-for="Participants" asp-items="Model.SelectOptions" multiple>
[BindProperty]
[Required(ErrorMessage = "* select at least one")]
public SelectList Participants { get; set; }
public List<SelectListItem> SelectOptions { get; set; }
OnPost I'd like to loop through all the selected items in the selectlist but when I try:
foreach (SelectListItem participant in Participants)
{
if (participant.Selected)
{
}
}
I get the error:
InvalidOperationException: Could not create an instance of type 'Microsoft.AspNetCore.Mvc.Rendering.SelectList'. Model bound complex types must not be abstract or value types and must have a parameterless constructor. Record types must have a single primary constructor. Alternatively, set the 'Participants' property to a non-null value in the 'ncswLearn2.Pages.Training.IndexModel' constructor.
So how should I loop through all of the selected values? I'm assuming there's a simple solution, but after googling for a bit I couldn't find the answer. Thanks in advance