Error: model item passed into the dictionary is of type '....List', but this dictionary requires a model of type '...IEnumerable`
I encountered this error while trying to render an asp.net MVC 3.0 application. I am working with DAB Architecture (3 projects) CAFM.Web, CAFM.Business, and CAFM.Data projects.
CAFM.Data includes
CAFMEntities : Entity FrameWork ITabMaster : Interface namespace CAFM.Data.Interfaces { public interface ITabMaster { TabMaster GetTabMasterById(Int64 id); IEnumerable FindTabMasterByName(string name); void AddTabMaster(TabMaster tabmaster); IEnumerable GetAllTabMaster(); } }
CAFM.Business includes reference of CAFM.Data namespace CAFM.Business.Repositories { public class TabMasterRepository : ITabMaster { public int colID { get; set; } public string FirstName { get; set; } public string LastName { get; set; }
private CAFMEntities _context;
public TabMasterRepository()
{
//IUnitOfWork unitOfWork = new CAFMEntities();
_context = new CAFMEntities();
}
public TabMasterRepository(IUnitOfWork unitOfWork)
{
if (unitOfWork == null)
throw new ArgumentNullException("unitOfWork");
_context = unitOfWork as CAFMEntities;
}
public IEnumera开发者_运维百科ble<TabMaster> GetAllTabMaster()
{
//(extension)List<Movie> IEnumerable<Movie>.ToList<Movie>()
//List<TabMaster> tabmasters = _context.TabMasters.ToList();
return _context.TabMasters.ToList() ;// .AsEnumerable<TabMaster>();//.ToList();
//return tabmasters;
}
public TabMaster GetTabMasterById(Int64 id)
{
return _context.TabMasters.Where(c => c.colID == id).Single();
}
public IEnumerable<TabMaster> FindTabMasterByName(string name)
{
return _context.TabMasters.Where(c => c.LastName.StartsWith(name)).AsEnumerable<TabMaster>(); //.ToList();
}
public void AddTabMaster(TabMaster tabmaster)
{
_context.TabMasters.AddObject(tabmaster);
}
}
}
CAFM.Web includes reference of CAFM.Data namespace CAFM.Web.Controllers { public class TabMasterController : Controller { // // GET: /TabMaster/
public ViewResult Index()
{
//Error comes in following line. (_context.TabMasters.ToList() is successfully return all the values but error is The model item passed into the dictionary is ... during rendering HTML
TabMasterRepository tr = new TabMasterRepository();
return View(tr.GetAllTabMaster());
}
}
}
Index.cshtml contains
@model IEnumerable @{ Layout = null; } TabMaster
List of Movies
@Html.ActionLink("Create New", "Create")
ID First Name Last Name @foreach (var item in Model) { @Html.DisplayFor(modelItem => item.colID) @Html.DisplayFor(modelItem => item.FirstName) @Html.DisplayFor(modelItem => item.LastName) }Error Description:
The model item passed into the dictionary is of type 'System.Collections.Generic.List1[CAFM.Data.TabMaster]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable
1[CAFM.Business.Repositories.TabMasterRepository]'.
Please provide your valuable suggestion to resolve above error.
Your answer would be appreciated! Thanks, Imdadhusen
I don't understand why do you want your repository in a view. Did you try to change model to
@model List<CAFM.Data.TabMaster>
In MVC you should pass data between controller and view thru ViewModels
精彩评论