title: InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'Microsoft.AspNetCore.Mvc.RedirectToActionResult',
body: <p>I'm learning Asp.net Core and building a simple web with CRUD operations, SQL server and using Entity Framework.</p>
  <p>when I try to add new products to test it  I git this error I tried many things to solve it but nothing changed so I need to figure out what's the problem, I know its betweenthe actionResult Add and the Add view :</p>
  <pre><code>InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'Microsoft.AspNetCore.Mvc.RedirectToActionResult', but this ViewDataDictionary instance requires a model item of type 'StockApp.Models.Product'.
  </code></pre>
  <p>that's my Product Model:</p>
  <pre><code>    using System.ComponentModel.DataAnnotations.Schema;
  using Microsoft.EntityFrameworkCore;
  using Microsoft.AspNetCore;
  using System.ComponentModel.DataAnnotations;
  using FluentValidation;
  
  namespace StockApp.Models
  {
      [Table(&quot;Products&quot;, Schema = &quot;dbo&quot;)]
      public class Product
      {
          [Key]
          [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
          [Display(Name = &quot;Product ID&quot;)]
          public int ProductId { get; set; }
  
  
          [Required]
          [Column(TypeName = &quot;varchar(150)&quot;)]
          [Display(Name = &quot;Product Name&quot;)]
          public string ProductName { get; set; }
  
          [Required]
          [DataType(DataType.Date)]
          [DisplayFormat(DataFormatString = &quot;{0:dd/MMM/yyyy}&quot;)]
          [Display(Name = &quot;Prodution Date&quot;)]
          public DateTime ProductionDate { get; set; }
  
  
          [Required]
          [DataType(DataType.Date)]
          [DisplayFormat(DataFormatString = &quot;{0:dd/MMM/yyyy}&quot;)]
          [Display(Name = &quot;Expiration Date&quot;)]
          public DateTime ExpirationDate { get; set; }
  
  
          [Required]
          [Column(TypeName = &quot;decimal(12,2)&quot;)]
          [Display(Name = &quot;Price&quot;)]
          public decimal Price { get; set; }
  
  
  
          //Category model/table
  
          [ForeignKey(&quot;Category&quot;)]
          [Required]
          public int CategoryId { get; set; }
  
          [Display(Name = &quot;Category&quot;)]
          [NotMapped]
          public string CategoryName { get; set; }
  
          public virtual Category Category { get; set; }
  
  
  
  
          Factory model/table
  
          [ForeignKey(&quot;Factory&quot;)]
          [Required]
          public int FactoryId { get; set; }
  
          [Display(Name = &quot;Made By&quot;)]
          [NotMapped]
          public string FactoryName { get; set; }
  
          public virtual Factory Factory { get; set; }
  
  
  
  
  
  
  
  
  
      }
  
  }    
  </code></pre>
  <p>that's my ProductController:</p>
  <pre><code>    using Microsoft.AspNetCore.Http;
  using Microsoft.AspNetCore.Mvc;
  using Microsoft.EntityFrameworkCore;
  using StockApp.Data;
  using StockApp.Models;
  using System.Linq;
  
  namespace StockApp.Controllers
  {
      public class ProductController : Controller
      {
          private readonly StockDbContext _dbContext;
  
  
          public ProductController(StockDbContext dbContext)
          {
              _dbContext = dbContext;
          }
  
  
  
          // GET: ProductController
          public IActionResult Index()
          {
              //var productDbContext = _dbContext.Products.Include(e =&gt; e.Category) .Include(e =&gt; e.Factory);
              //return View(productDbContext.ToList());
  
              var productss = (from Product in _dbContext.Products
                               join Category in _dbContext.Categories on Product.ProductId equals Category.CategoryId
                               join Factory in _dbContext.Factories on Product.ProductId equals Factory.FactoryId
                               select new Product
                               {
                                   ProductId = Product.ProductId,
                                   ProductName = Product.ProductName,
                                   ProductionDate = Product.ProductionDate,
                                   ExpirationDate = Product.ExpirationDate,
                                   CategoryId = Product.CategoryId,
                                   CategoryName = Category.CategoryName,
                                   FactoryId = Product.FactoryId,
                                   FactoryName = Factory.FactoryName
  
  
  
  
                               }).ToList();
              return View(productss);
          }
  
           
  
          // GET: ProductController/Details/5
          public ActionResult Details(int id)
          {
              return View();
          }
  
  
  
  
  
          // GET: ProductController/Create
          public IActionResult Add()
          {
              ViewBag.Category = this._dbContext.Categories.ToList();
              ViewBag.Factory = this._dbContext.Factories.ToList();
              return View();
          }
  
          // POST: ProductController/Create
          [HttpPost]
          public IActionResult Add(Product model)
          {
              ModelState.Remove(&quot;Category&quot;);
              ModelState.Remove(&quot;CategoryName&quot;);
              ModelState.Remove(&quot;Factory&quot;);
              ModelState.Remove(&quot;FactoryName&quot;);
              if (ModelState.IsValid)
              {
                  _dbContext.Products.Add(model);
                  _dbContext.SaveChanges();
                  return View(RedirectToAction(&quot;Index&quot;));
              }
              ViewBag.Category = _dbContext.Categories.ToList();
              ViewBag.Factory = _dbContext.Factories.ToList();
              return View(&quot;Add&quot;, model);
          }
  
  
  
  
          // GET: ProductController/Edit/5
          public IActionResult Edit(int id)
          {
              return View();
          }
  
          // POST: ProductController/Edit/5
          [HttpPost]
          [ValidateAntiForgeryToken]
          public ActionResult Edit(int id, IFormCollection collection)
          {
              try
              {
                  return RedirectToAction(nameof(Index));
              }
              catch
              {
                  return View();
              }
          }
  
          // GET: ProductController/Delete/5
          public ActionResult Delete(int id)
          {
              return View();
          }
  
          // POST: ProductController/Delete/5
          [HttpPost]
          [ValidateAntiForgeryToken]
          public ActionResult Delete(int id, IFormCollection collection)
          {
              try
              {
                  return RedirectToAction(nameof(Index));
              }
              catch
              {
                  return View();
              }
          }
      }
  }
  </code></pre>
  <p>Add View:</p>
  <pre><code>@model Product
  
  
  
  @{
      ViewBag.Title = &quot;Add&quot;;
  }
  
  &lt;h2&gt;@ViewBag.Title Product&lt;/h2&gt;
  
  &lt;h4&gt;Product&lt;/h4&gt;
  &lt;div class=&quot;row&quot;&gt;
      &lt;div class=&quot;col-md-4&quot;&gt;
          &lt;form asp-action=@ViewBag.Title&gt;
              &lt;div asp-validation-summary=All class=&quot;text-danger&quot;&gt;&lt;/div&gt;
              &lt;div class=&quot;form-group&quot;&gt;
                  &lt;label asp-for=&quot;ProductName&quot; class=&quot;control-label&quot;&gt;&lt;/label&gt;
                  &lt;input asp-for=&quot;ProductName&quot; class=&quot;form-control&quot; /&gt;
                  &lt;span asp-validation-for=&quot;ProductName&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;ProductionDate&quot; class=&quot;control-label&quot;&gt;&lt;/label&gt;
                  &lt;input asp-for=&quot;ProductionDate&quot; class=&quot;form-control&quot; /&gt;
                  &lt;span asp-validation-for=&quot;ProductionDate&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;ExpirationDate&quot; class=&quot;control-label&quot;&gt;&lt;/label&gt;
                  &lt;input asp-for=&quot;ExpirationDate&quot; class=&quot;form-control&quot; /&gt;
                  &lt;span asp-validation-for=&quot;ExpirationDate&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;Price&quot; class=&quot;control-label&quot;&gt;&lt;/label&gt;
                  &lt;input asp-for=&quot;Price&quot; class=&quot;form-control&quot; /&gt;
                  &lt;span asp-validation-for=&quot;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;CategoryId&quot; class=&quot;control-label&quot;&gt;&lt;/label&gt;
                  &lt;select asp-for=&quot;CategoryId&quot;  asp-items=&quot;@(new SelectList(ViewBag.Category,&quot;CategoryId&quot;,&quot;CategoryName&quot;))&quot; class =&quot;form-control&quot;&gt;&lt;/select&gt;
              &lt;/div&gt;
              &lt;div class=&quot;form-group&quot;&gt;
                  &lt;label asp-for=&quot;FactoryId&quot; class=&quot;control-label&quot;&gt;&lt;/label&gt;
                  &lt;select asp-for=&quot;FactoryId&quot;  asp-items=&quot;@(new SelectList(ViewBag.Factory,&quot;FactoryId&quot;,&quot;FactoryName&quot;))&quot; class =&quot;form-control&quot;&gt;&lt;/select&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;
  </code></pre>
  <p>index view :</p>
  <pre><code>@model IEnumerable&lt;StockApp.Models.Product&gt;
  
  @{
      ViewBag.Title = &quot;Product&quot;;
  }
  
  &lt;h1&gt;All Productts&lt;/h1&gt;
  
  &lt;p&gt;
      &lt;a asp-action=&quot;Add&quot; class=&quot;btn btn-outline-primary&quot;&gt;Add New Product&lt;/a&gt;
  &lt;/p&gt;
  &lt;table class=&quot;table&quot;&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;
                  @Html.DisplayNameFor(model =&gt; model.ProductName)
              &lt;/th&gt;
              &lt;th&gt;
                  @Html.DisplayNameFor(model =&gt; model.ProductionDate)
              &lt;/th&gt;
              &lt;th&gt;
                  @Html.DisplayNameFor(model =&gt; model.ExpirationDate)
              &lt;/th&gt;
              &lt;th&gt;
                  @Html.DisplayNameFor(model =&gt; model.Price)
              &lt;/th&gt;
              &lt;th&gt;
                  @Html.DisplayNameFor(model =&gt; model.CategoryName)
              &lt;/th&gt;
              
             &lt;th&gt;
                  @Html.DisplayNameFor(model =&gt; model.FactoryName)
              &lt;/th&gt;
              
              &lt;th&gt;&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
  @foreach (var item in Model) {
          &lt;tr&gt;
              &lt;td&gt;
                  @Html.DisplayFor(modelItem =&gt; item.ProductName)
              &lt;/td&gt;
              &lt;td&gt;
                  @Html.DisplayFor(modelItem =&gt; item.ProductionDate)
              &lt;/td&gt;
              &lt;td&gt;
                  @Html.DisplayFor(modelItem =&gt; item.ExpirationDate)
              &lt;/td&gt;
              &lt;td&gt;
                  @Html.DisplayFor(modelItem =&gt; item.Price)
              &lt;/td&gt;
              &lt;td&gt;
                  @Html.DisplayFor(modelItem =&gt; item.CategoryName)
              &lt;/td&gt;
              
              &lt;td&gt;
                  @Html.DisplayFor(modelItem =&gt; item.FactoryName)
              &lt;/td&gt;
             
              &lt;td&gt;
                  &lt;a asp-action=&quot;Edit&quot; asp-route-id=&quot;@item.ProductId&quot;&gt;Edit&lt;/a&gt; |
                  &lt;a asp-action=&quot;Details&quot; asp-route-id=&quot;@item.ProductId&quot;&gt;Details&lt;/a&gt; |
                  &lt;a asp-action=&quot;Delete&quot; asp-route-id=&quot;@item.ProductId&quot;&gt;Delete&lt;/a&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
  }
      &lt;/tbody&gt;
  &lt;/table&gt;
  </code></pre>