title: Submitting Form from ViewModel gives InvalidOperationException
body: <p>I've been pulling my hair out over this error for hours now. I'm trying to create a detailed search page from a ViewModel and one of the properties 'Category' is pulled from a database and put into a dropdownlist. The Detailed Search page renders fine, however, when the form is submitted it give me the error below instead of returning to the index.cshtml page like it should.</p>
  <p>This is the error:</p>
  <pre><code>InvalidOperationException: The ViewData item that has the key 'CategoryID' is of type 'System.Int32' but must be of type 'IEnumerable&lt;SelectListItem&gt;'.
  </code></pre>
  <p>Here is the Controller Code: (HomeController.cs)</p>
  <pre><code>    public IActionResult DetailedSearch()
          {
              ViewBag.AllCategories = GetAllCategories();
  
              SearchViewModel svm = new SearchViewModel();
              svm.CategoryID = 0;
             
              return View(svm);
          }
  
          private SelectList GetAllCategories()
          {
              //Get the list of categories from the database
              List&lt;Category&gt; categoryList = _context.Categories.ToList();
  
              //add a dummy entry so the user can select all categories
              Category SelectALL = new Category() { CategoryID = 0, CategoryName = &quot;All Categories&quot; };
              categoryList.Add(SelectALL);
  
              //convert the list to a SelectList by calling SelectList constructor
              //CategoryID and CategoryName are the names of the properties on the Category class
              //CategoryID is the primary key
              SelectList categorySelectList = new SelectList(categoryList.OrderBy(c =&gt; c.CategoryID), &quot;CategoryID&quot;, &quot;CategoryName&quot;);
  
              //return the SelectList
              return categorySelectList;
  
          }
  
          public IActionResult DisplaySearchResults(SearchViewModel svm)
          {
  
              //Checks to see if data was entered correctly
  
              TryValidateModel(svm);
  
              if (!ModelState.IsValid)
              {
                  return View(&quot;DetailedSearch&quot;, svm);
              }
  
              return View(&quot;Index&quot;);
          }
  </code></pre>
  <p>The View Model: SearchViewModel.cs</p>
  <pre><code>public class SearchViewModel
  {
      [Display(Name = &quot;Title&quot;)]
      public String Title { get; set; }
  
      [Display(Name = &quot;Cast&quot;)]
      public String Cast { get; set; }
  
      [Display(Name = &quot;Select a Starting Date&quot;)]
      [DataType(DataType.Date)]
      public DateTime? Date { get; set; }
  
      [Display(Name = &quot;Year&quot;)]
      [Range(minimum: 1900, maximum: 2021, ErrorMessage = &quot;Select a Number between 1900 and 2021&quot;)]
      public Int32 Year { get; set; }
  
      [Display(Name = &quot;Type of Search&quot;)]
      public DateSearchType SelectedDateSearchType { get; set; }
  
      [Display(Name = &quot;Ratings&quot;)]
      public Rating? Rating { get; set; }
  
      [Display(Name = &quot;Categories&quot;)]
      public Int32? CategoryID { get; set; }
  
      [Display(Name = &quot;Description&quot;)]
      public String Description { get; set; }
  
  }
  </code></pre>
  <p>The DetailedSearch View:</p>
  <pre><code>        &lt;div class=&quot;form-group&quot;&gt;
              &lt;label asp-for=&quot;CategoryID&quot; class=&quot;control-label&quot;&gt;&lt;/label&gt;
              @Html.DropDownListFor(model =&gt; model.CategoryID, (SelectList)ViewBag.AllCategories, new { @class = &quot;form-control&quot; })
              &lt;span asp-validation-for=&quot;CategoryID&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
          &lt;/div&gt;
  </code></pre>
  <p>I know there are a ton of similar posts, but none of them have seemed to help me, I've also sat down with my TA for 45 minutes and was unable to figure it out. So any help would be appreciated.</p>