title: MVC Error - An unhandled exception occurred while processing the request. InvalidOperationException: The model passed
body: <p>I'm having the following error when I'm trying to create a product:</p>
  <blockquote>
  <p>An unhandled exception occurred while processing the request.
  InvalidOperationException: The model item passed into the
  ViewDataDictionary is of type 'Library.Models.Wine.Product', but this
  ViewDataDictionary instance requires a model item of type
  'Library.ViewModels.ProductViewModel'.</p>
  </blockquote>
  <p><em><strong>Where is the problem in my code? Does anyone know?</strong></em></p>
  <p>Here is the code:</p>
  <p><strong>My Product Service:</strong></p>
  <pre><code>public async Task InsertAsync(Product obj)
  {
      _context.Add(obj);
      await _context.SaveChangesAsync();
  }
  </code></pre>
  <p><strong>My Product ViewModel:</strong></p>
  <pre><code>public Product products { get; set; }
  public virtual ICollection&lt;WineBrand&gt; wine_brands { get; set; }
  public virtual ICollection&lt;Harvest&gt; harvests { get; set; }
  </code></pre>
  <p><strong>My Product Controller:</strong></p>
  <pre><code>        private readonly ProductService _productService;
          private readonly WineBrandService _wineBrandService;
          private readonly HarvestService _harvestService;
  
          public ProductsController( 
              ProductService productService, 
              WineBrandService wineBrandService, 
              HarvestService harvestService)
          {
              _productService = productService;
              _wineBrandService = wineBrandService;
              _harvestService = harvestService;
  
          }
  
      public async Task&lt;IActionResult&gt; Create()
      {
          var wine = await _wineBrandService.FindAllAsync();
          var year = await _harvestService.FindAllAsync();
  
          var viewmodel = new ProductViewModel 
          { 
              wine_brands = wine,
              harvests = year
  
          };
  
          return View(viewmodel);
      }
  
      [HttpPost]
      [ValidateAntiForgeryToken]
      public async Task&lt;IActionResult&gt; Create(Product product)
      {
          if (!ModelState.IsValid)
          {
              return View(product);
          }
  
          await _productService.InsertAsync(product);
          return RedirectToAction(nameof(Index));
      }
  </code></pre>
  <p><strong>My Product Create View:</strong></p>
  <pre><code>   @model Library.ViewModels.ProductViewModel
  
  @{
      ViewData[&quot;Title&quot;] = &quot;Create&quot;;
  }
  
  &lt;h1&gt;Create&lt;/h1&gt;
  
  &lt;h4&gt;Employee&lt;/h4&gt;
  &lt;hr /&gt;
  &lt;div class=&quot;row&quot;&gt;
      &lt;div class=&quot;col-md-4&quot;&gt;
          &lt;form asp-action=&quot;Create&quot;&gt;
              &lt;div asp-validation-summary=&quot;ModelOnly&quot; class=&quot;text-danger&quot;&gt;&lt;/div&gt;
              &lt;div class=&quot;form-group&quot;&gt;
                  &lt;label asp-for=&quot;products.Name&quot; class=&quot;control-label&quot;&gt;&lt;/label&gt;
                  &lt;input asp-for=&quot;products.Name&quot; class=&quot;form-control&quot; /&gt;
                  &lt;span asp-validation-for=&quot;products.Name&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
              &lt;/div&gt;
  
              &lt;div class=&quot;form-group&quot;&gt;
                  &lt;label asp-for=&quot;products.Price&quot; class=&quot;control-label&quot;&gt;&lt;/label&gt;
                  &lt;input asp-for=&quot;products.Price&quot; class=&quot;form-control&quot; /&gt;
                  &lt;span asp-validation-for=&quot;products.Price&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
              &lt;/div&gt;
  
              &lt;div class=&quot;form-group&quot;&gt;
                  &lt;label asp-for=&quot;products.WineBrandId&quot; class=&quot;control-label&quot;&gt;&lt;/label&gt;
                  &lt;select asp-for=&quot;products.WineBrandId&quot; asp-items=&quot;@(new SelectList(Model.wine_brands, &quot;Id&quot;, &quot;Name&quot;))&quot; class=&quot;form-control&quot;&gt;&lt;/select&gt;
                  &lt;span asp-validation-for=&quot;products.Brand&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
              &lt;/div&gt;
  
              &lt;div class=&quot;form-group&quot;&gt;
                  &lt;label asp-for=&quot;products.HarvestId&quot; class=&quot;control-label&quot;&gt;&lt;/label&gt;
                  &lt;select asp-for=&quot;products.HarvestId&quot; asp-items=&quot;@(new SelectList(Model.harvests, &quot;Id&quot;, &quot;Year&quot;))&quot; class=&quot;form-control&quot;&gt;&lt;/select&gt;
                  &lt;span asp-validation-for=&quot;products.Harvest&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
              &lt;/div&gt;
  
              &lt;div class=&quot;form-group&quot;&gt;
                  &lt;input type=&quot;submit&quot; value=&quot;Create&quot; class=&quot;btn btn-primary&quot; /&gt;
              &lt;/div&gt;
          &lt;/form&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  
  &lt;div&gt;
      &lt;a asp-action=&quot;Index&quot;&gt;Back to List&lt;/a&gt;
  &lt;/div&gt;
  
  @section Scripts {
      @{await Html.RenderPartialAsync(&quot;_ValidationScriptsPartial&quot;);}
  }
  </code></pre>