title: How to save data from multiple selection dropdown
body: <p>I started learning about the entity framework and asp.net and I wanted to use all CRUD operations.
  I have Task and Category models and they are in many-to-many relationship. In my database there are 3 tables: 1.<b>Task</b> (it contains Id, Title, Description..), 2.<b>Category</b> (it contains categoryId and categoryName) and 3.<b>TaskCategory</b> (contains Id (taskId) and categoryId). </p>
  
  <p>I managed to do everything I wanted except to add multiple categories to task using dropdown. I created dropdown, and loaded categories and I know how to add one category to task (when relation is 1:N) (<code>asp-for="CategoryId"</code>). For multiple selection I tried with <code>selectedCategories</code> (list od integers - ids) instead of CategoryId but I dont know what to do with that list. How to join category and task in TaskCategory while saving task in controller (in other words: how to save categories to task)?</p>
  
  <p>AddTask.cshtml</p>
  
  <pre class="lang-html prettyprint-override"><code>&lt;div class="form-group"&gt;
      &lt;label class="control-label"&gt;Category&lt;/label&gt;
      &lt;select class="select-picker" asp-for="selectedCategories"
          asp-items="@(new SelectList(Model.Categories, "CategoryId", "CategoryName"))" multiple&gt;
      &lt;/select&gt;
  &lt;/div&gt;
  </code></pre>
  
  <pre class="lang-html prettyprint-override"><code>&lt;script&gt;
      ...
       $('.select-picker').selectpicker('toggle');
  &lt;/script&gt;
  </code></pre>
  
  <p>HomeController.cs</p>
  
  <pre><code>public IActionResult AddTask()
  {
      var categories = _categoryRepository.GetAllCategories().OrderBy(c =&gt; c.CategoryName);
      var taskCategories = _taskCategoryRepository.GetAllTaskCategories().OrderBy(tc =&gt; tc.Id);
  
      var homeViewModel = new HomeViewModel()
      {
          Task = null,
          TaskCategory = null,
          Categories = categories.ToList(),
          TaskCategories = taskCategories.ToList(),
          selectedCategories = new List&lt;int&gt;()
      };
  
      return View(homeViewModel);
  }
  
  [HttpPost]
  public IActionResult AddTask(Task task, List&lt;int&gt; selected)
  {
      // foreach (var selectedCategoryId in selected)
      // {
      //
      // }
  
      _taskRepository.AddTask(task);
      return RedirectToAction("Index");
  
      return View();
  }
  </code></pre>
  
  <p>This way I get Task in database, but ofcourse without any category saved.</p>