【问题标题】:The model item passed into the dictionary is of type { ASP.NET MVC }传入字典的模型项的类型为 { ASP.NET MVC }
【发布时间】:2010-06-30 15:55:24
【问题描述】:

我在查看页面上出现错误,我认为我传递了错误的类型,任何人都可以纠正我 - 谢谢。

传入字典的模型项的类型为“System.Collections.Generic.List1[App.Domain.Model.Interface.IPerson]' but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[App.Domain.Service.PersonService]”。

MyService 看起来像这样......

namespace App.Domain.Service
{
    public class PersonService :  IPersonService
    { 
        private IPersonRepository _personRepository;
        public PersonService(IPersonRepository personRepository)
        {
            _personRepository = personRepository; 
        }

        public List<IPerson> GetPerson()
        {            
            return _personRepository.GetHost();
        } 
    }
}

myView 看起来像这样...

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" 
Inherits="System.Web.Mvc.ViewPage<IEnumerable<App.Domain.Service.PersonService>>" %>

我的控制器看起来像这样...

 public ActionResult GetPersons()
        {
            IPersonRepository personRepo = new PersonRepository();
            PersonService person = new PersonService(personRepo);
            IList<IPerson> p  = person.GetPerson(); 
            return View(p);
        }

【问题讨论】:

    标签: asp.net asp.net-mvc


    【解决方案1】:

    您向 View() 发送了错误的类型 在您的视图中声明说 App.Domain.Service.PersonService,但在您的视图中,您正在向它发送一个视图(IPerson)。 如果您想将 IPerson 发送到您的视图,则标签应如下所示:

    <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<App.Domain.Model.Interface.IPerson>>" %> 
    

    【讨论】:

    • 为什么我在视图数据类中看不到“App.Domain.Model.Interface.IPerson”?意味着...当我创建一个视图页面(在控制器中右键单击并单击添加视图)并选择“创建强类型视图”但我看到了我所有的模型/存储库类但我没有看到与“接口”相关的任何内容这是为什么呢?
    • 不太确定为什么它们不包含接口,实际上我之前没有尝试通过视图向导使用一个。不过,这将是一个很好的后续问题。
    猜你喜欢
    • 2014-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-05
    • 2019-06-01
    • 2011-11-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多