I am getting the following error. Can anyone help me solve this error? The code is: -
public class CategoryController : Controller
{
//
// GET: /Category/
ProductsDbEntities ctx;
public CategoryController()
{
ctx = new ProductsDbEntities();
}
public ActionResult Index()
{
try
{
var categoryList = ctx.Categories.ToList();
return View(categoryList);
}
catch (Exception ex)
{
throw ex;
}
}
The code for the View is: -
@model MvcSession.Models.CategoryDetails
@{
ViewBag.Title = "SearchProducts";
}
@using (Html.BeginForm())
{
@Html.EditorFor(cat => cat.CategoryId);
I am getting the following error when I run the application :-
The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[MvcSession.EntityModel.Category]', but this dictionary requires a model item of type 'MvcSession.Models.CategoryDetails'.
Mukesh NayakPosted Apr 15, 2015, 11:34 AM
I think in the statement
var categoryList = ctx.Categories.ToList();
return View(categoryList);
you are passing List
@model MvcSession.Models.CategoryDetails
Probably you should get first or default like following
var selectedCategory= ctx.Categories.FirstOrdefault();
Again you have to map the selected Category with that of MvcSession.Models.CategoryDetails
as following
var categorydetail = new MvcSession.Models.CategoryDetails ();
categoryDetail.property1 = selectedcategory.Property1;
categoryDetail.property2 = selectedcategory.Property2;
Finally
return View(categorydetail );
Hope this will help !!
Guest UserPosted Apr 15, 2015, 9:24 AM
@model MvcSession.Models.CategoryDetails
but pass Categories. Modify the above to:
@model MvcSession.Models.Category