I have a Razor View in an Asp.Net Core Mvc project that takes a SelectList object as the model but I do not know how to then process the user selected items. Here is where I've got to so far:
The View Model I use to retrieve data to populate the dropdown list
public class PlayersListViewModel
{
public int id { get; set; }
public string name { get; set; }
public int gender { get; set; }
public DateTime dob { get; set; }
public string skill { get; set; }
public double rating { get; set; }
}
My Controller method that gets the data and then builds a SelectList as a model for the View
public ViewResult SelectEntrants()
{
IEnumerable<PlayersListViewModel> playersList = new List<PlayersListViewModel>().ToList();
playersList = _employeeRepository.PlayersList().ToList();
return View(new SelectList(playersList, "id", "name"));
}
The View
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@model SelectList
@{
ViewBag.Title = "Select Entrants";
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<form method="post" asp-controller="Functions" asp-action="SelectEntrants">
<select id="id" multiple name="name" asp-items="Model" style="height:500px">
<option value="0">Please select</option>
</select>
<a asp-controller="functions" asp-action="showentrants"
class="btn btn-primary m-1">Show entrants count</a>
</form>
</body>
</html>
Using the above I get a multi-select dropdown in the View containing a list of names - great. But I have no idea how to get a list of selected items once the page is posted back to the server - I just can't find an example of a controller method that shows how to do that. Can anyone help?