Asp.Net core y select multiple

Html helper c# select multiple asp .net core

Crear un elemento select multiple de html. Utilizamos asp.net core MVC con DropDownListFor. En este ejemplo asignamos a mano los datos y valores del select.

Crear viewmodel

namespace WebApplication2.Models
{
    public class AboutViewModel
    {
        public int[] Values { get; set; }
        public Dictionary<int, string> Options { get; set; }
    }
}

Crear select multiple en la vista razor con html helper

@using (Html.BeginForm())
{
    @Html.DropDownListFor(a => a.Values, new SelectList(Model.Options, "Key", "Value"), "SELECCIONAR", new { @multiple = "multiple" })
}

Configurar controlador

public IActionResult About()
{
	AboutViewModel model = new AboutViewModel();

	model.Options = new Dictionary<int, string>();
	model.Options.Add(1, "Option 1");
	model.Options.Add(2, "Option 2");
	model.Options.Add(3, "Option 3");


	ViewData["Message"] = "Your application description page.";
	return View(model);
}

[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult About(AboutViewModel model)
{
	return View();
}

Obtener datosAsp .net core debug visual studio select multiple

Eso es todo. Fácil y rápido.