title: How to use multi selection in ASP.NET MVC application?
body: <p>Two days already I am trying to figure out and learn how to build multi-selection dropdown list in ASP.NET Core, that is seeded from db context, and results are used further in the constructor, so I decided to ask this question. I did not find anywhere how to use data from posted form, so maybe you will be able to help me. I belive that I did some mistakes in my POST action in controller. I am not sure how I am supposed to get posted data into the controller.</p>
  
  <p>I was trying various combinations on simple example. Best what I have now is:</p>
  
  <p>ViewModel.cs:</p>
  
  <pre><code>public class ProductViewModel
      {
          public List&lt;SelectListItem&gt; Products { get; set; }
          public int[] ProductIDs { get; set; }
      }
  </code></pre>
  
  <p>Index.cshtml:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;Aplikacja&lt;/title&gt;
      &lt;meta charset="utf-8"&gt;
      &lt;link rel="stylesheet" href="~/dist/allstyles.css" /&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form asp-action="Index" method="post"&gt;
          &lt;input type="hidden" asp-for="Products" /&gt;
  
          &lt;div class="form-group"&gt;
              &lt;label asp-for="Products"&gt;Produkty&lt;/label&gt;
              &lt;select asp-for="ProductIDs" class="form-control" asp-items="ViewBag.Lista"&gt;&lt;/select&gt;
          &lt;/div&gt;
          &lt;div class="text-center"&gt;
              &lt;button class="btn btn-primary" type="submit"&gt;Save&lt;/button&gt;
              &lt;a asp-action="Index" class="btn btn-secondary"&gt;Cancel&lt;/a&gt;
          &lt;/div&gt;
      &lt;/form&gt;
  
  //ViewBag tests from controller
      @ViewBag.Message  //test1, returns 0
      @ViewBag.Message2 //test2, returns 0
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Controller.cs</p>
  
  <pre><code>public class ProductController : Controller
  {
      private readonly ApplicationDbContext _context; //database context
  
      public ProductController(ApplicationDbContext context)
      {
          _context = context; //context for controller
      }
      //GET
      public ActionResult Index()
      {
          ProductViewModel productVM = new ProductViewModel(); //viewmodel instance
          PopulateProducts(); //populating dropdown
          return View(productVM); //returning viewmodel
      }
      //POST (I belive that here I have screwed something a lot, I do not understand everything what is going on in this action)
      [HttpPost]
      public ActionResult Index(ProductViewModel productVM)
      {
          PopulateProducts(); 
  
          if (productVM.ProductIDs !=null)
          {
              List&lt;SelectListItem&gt; selectedItems = productVM.Products.Where(p =&gt; productVM.ProductIDs.Contains(int.Parse(p.Value))).ToList();
  
              ViewBag.Message = selectedItems.Count.ToString(); //returns 0
              ViewBag.Message2 = productVM.Products.Count.ToString();//returns 0
          }
          return View(productVM);
      }
  
      private void PopulateProducts() //method populating dropdown list
      {
  
          var techQuery = from t in _context.Products //getting products context from database
                          orderby t.Name //sorting by names
                          select t; //selecting item (??)
  
          if (techQuery != null) //if db context var is not null...
          {
              ViewBag.Lista = techQuery.Select(n =&gt; new SelectListItem { Text = n.Name, Value = n.ProductID.ToString() }); //...creating viewbag that is used for populating dropdown list in view
  
          }
      }
  }
  </code></pre>