【问题标题】:Error: model item passed into the dictionary is of type '....List', but this dictionary requires a model of type '...IEnumerable`错误:传入字典的模型项的类型为“....List”,但此字典需要“...IEnumerable”类型的模型
【发布时间】:2011-09-06 20:53:15
【问题描述】:

我在尝试渲染 asp.net MVC 3.0 应用程序时遇到了这个错误。 我正在使用 DAB 架构(3 个项目)CAFM.Web、CAFM.BusinessCAFM.Data 项目。

CAFM.Data 包括

CAFMEntities : 实体框架 ITabMaster : 接口 命名空间 CAFM.Data.Interfaces { 公共接口 ITabMaster { TabMaster GetTabMasterById(Int64 id); IEnumerable FindTabMasterByName(字符串名称); void AddTabMaster(TabMaster tabmaster); IEnumerable GetAllTabMaster(); } }

CAFM.Business 包括 CAFM.Data的参考 命名空间 CAFM.Business.Repositories { 公共类 TabMasterRepository : ITabMaster { 公共 int colID { 获取;放; } 公共字符串名字 { 获取;放; } 公共字符串姓氏{得到;放; }

    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 IEnumerable<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 包括 CAFM.Data的参考 命名空间 CAFM.Web.Controllers { 公共类 TabMasterController : 控制器 { // // 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 包含

@model IEnumerable @{ 布局=空; } 标签大师

电影列表

@Html.ActionLink("新建", "创建")

ID 名 姓 @foreach(模型中的变量项) { @Html.DisplayFor(modelItem => item.colID) @Html.DisplayFor(modelItem => item.FirstName) @Html.DisplayFor(modelItem => item.LastName) }

错误说明: 传入字典的模型项的类型为“System.Collections.Generic.List1[CAFM.Data.TabMaster]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[CAFM.Business.Repositories.TabMasterRepository]”。

请提供您宝贵的建议以解决上述错误。

您的回答将不胜感激! 谢谢, 伊姆达杜森

【问题讨论】:

    标签: model-view-controller


    【解决方案1】:

    我不明白您为什么要在视图中显示您的存储库。您是否尝试将模型更改为

    @model List<CAFM.Data.TabMaster> 
    

    在 MVC 中,您应该通过 ViewModels 在控制器和视图之间传递数据

    【讨论】:

    • 你是对的。您能否为我提供使用(.Web、.Business 和 .Data)架构的 MVC 3.0 示例应用程序?如果可以,请帮助我!
    • 很抱歉,如果您在asp.net 网站上没有您要查找的内容,我无法帮助您,因为我知道这是使用实体框架的唯一内容。尝试更多地谷歌并在stackoverflow上查看here。你应该在 nerddinner.com 上练习 here 是 MVC 2.0 和 walkthru 在 MVC 1 中的来源,但它们仍然相关。如果你想在 MVC 2 中进行遍历,你必须购买 book
    猜你喜欢
    • 2023-03-30
    • 2020-09-08
    • 2013-10-18
    • 1970-01-01
    • 1970-01-01
    • 2013-10-17
    • 1970-01-01
    • 2015-09-27
    • 2014-02-02
    相关资源
    最近更新 更多