Running this Index controller gives me errors, when I try to pass IEnumerable into Tempdata.
When debugging, TempData["ProductCategory"] seems to store expected data in Watch, but cannot render Html View after storing. However, the Html does show when I store a simple stirng.
public IActionResult Index()
{
// This gives me http error 500
//TempData["ProductCategory"] = productcategoryrepository.GetAllProductCategory();
// This works at least!
TempData["ProductCategory"] = "test";
//TempData.Keep();
return View();
}
public IEnumerable<ProductCategory> GetAllProductCategory()
{
return _context.ProductCategory.ToList();
}
Other Info:
public partial class ProductCategory
{
public ProductCategory()
{
Product = new HashSet<Product>();
}
public int ProductCategoryId { get; set; }
public string ProductCategoryName { get; set; }
public string ProductCategoryDescription { get; set; }
public virtual ICollection<Product> Product { get; set; }
}
public partial class Product
{
public int ProductId { get; set; }
public string ProductName { get; set; }
public string ProductDescription { get; set; }
public string ImageLocation { get; set; }
public int? ProductCategoryId { get; set; }
public virtual ProductCategory ProductCategory { get; set; }
}
To be able to store data in sessions or temp data, the type must be serializable. IEnumerable<T>
cannot be serialized. Try a List<T>
instead.
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/app-state?view=aspnetcore-2.1