title: ASP.Net Core 3.1 MVC - MultiSelect with List of Objects
body: <p>So, basically the aim is:</p>
  <p>User selects multiple 'categories' for a book (genres) from a select list. Some examples are:</p>
  <ul>
  <li>Action and Adventure</li>
  <li>Classics</li>
  <li>Comic Book or Graphic Novel</li>
  </ul>
  <p>After the user selects 1 or more (so multiple), they save the book to a database.</p>
  <p>Functiality wise, everything worked just fine when I was dealing with singular genres (the genre was saved as a single string), but I need to have support for multiple genres.</p>
  <p>The first thing I did was simply change the <code>string Category</code> to <code>List&lt;string&gt; Categories</code>, but that caused the error seen here: <a href="https://stackoverflow.com/questions/51659280/the-property-propertyname-could-not-be-mapped-because-it-is-of-type-listdec">StackOverflow issue, which was solved</a>
  I tried the solution that 'TuPack' offered, and that error went away... but it led to a whole new issue.</p>
  <p>You're up to speed on where I am now:
  In order to get any results out of my <code>&lt;select&gt;&lt;/select&gt; </code>, I have to do this:</p>
  <pre><code>&lt;select asp-for=&quot;CategoryIds[0].CategoryId&quot; asp-items=&quot;ViewBag.Language&quot;
          class=&quot;form-control&quot; multiple=&quot;multiple&quot;&gt;
     &lt;option value=&quot;&quot;&gt;Please choose book language&lt;/option&gt;                    
  &lt;/select&gt;
  </code></pre>
  <p>That certainly works.. for getting the first category the user selects. That's all though.</p>
  <p>As for the code so far, relevance wise... I'll cut off the db related stuff, since the issue occurs long before there:</p>
  <p>Controller:</p>
  <pre><code>public async Task&lt;ViewResult&gt; AddNewBookAsync(bool isSuccess = false, int bookId = 0)
          {
              var model = new BookModel();
              
              ViewBag.Language = new SelectList(await _languageRepository.GetLanguages(), &quot;Id&quot;, &quot;Name&quot;);
  
              ViewBag.IsSucess = isSuccess;
              ViewBag.BookId = bookId;            
              return View(model);
          }
  
          [HttpPost]
          public async Task&lt;IActionResult&gt; AddNewBook(BookModel bookModel)
          {
  
  
              if (ModelState.IsValid)
              {
                  int id = await _bookRepository.AddNewBook(bookModel);
                  if(id &gt; 0)
                  {
                      return RedirectToAction(nameof(AddNewBook), new { isSuccess = true, bookId = true });
                  }
              }
  
  
  
              ViewBag.Language = new SelectList(await _languageRepository.GetLanguages(), &quot;Id&quot;, &quot;Name&quot;);
              return View();
          }
  </code></pre>
  <p>BookModel:</p>
  <pre><code>public class BookModel
      {
          public int Id { get; set; }
          [StringLength(100, MinimumLength = 5)]
          [Required(ErrorMessage =&quot;Please enter the title of your book&quot;)]
          public string Title { get; set; }
          [Required(ErrorMessage = &quot;Please enter the author's name&quot;)]
          public string Author { get; set; }
          [StringLength(500)]
          public string Description { get; set; }
          
          [Required(ErrorMessage = &quot;Please choose the language of your book&quot;)]
          public string Language { get; set; }
          [Display(Name = &quot;Genre&quot;)]
          public List&lt;Categories&gt; CategoryIds { get; set; } = new List&lt;Categories&gt;();
  
          [Required(ErrorMessage = &quot;Please enter the total pages&quot;)]
          [Display(Name = &quot;Total pages of book&quot;)]
          public int? TotalPages  { get; set; }
      }
  </code></pre>
  <p>I showed the relevant Html code previously, so won't post it again.</p>
  <p>Visuals:
  Example Input:
  <a href="https://i.stack.imgur.com/y38em.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/y38em.png" alt="Example Input" /></a>
  Current Output:
  <a href="https://i.stack.imgur.com/Kq79E.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Kq79E.png" alt="Current Output" /></a></p>
  <p>Category Id 1 is 'Action And Adventure'.</p>
  <p>By the way, I'm sure it is quite easy to tell, but this is just a 'practice project'. I don't want to make big changes to the main project I'm working on without actually knowing what I'm doing.</p>
  <p>Update:
  After changing <code>CategoryIds[0].CategoryId</code> and running it with the exact same values as before, I get:
  <a href="https://i.stack.imgur.com/PSeG5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PSeG5.png" alt="0 object results" /></a></p>