2011-09-06 7 views
0

제네릭 컬렉션의 문제점을 이해할 수 있도록 도와 주시겠습니까? 미리 감사드립니다!ASP.Net MVC 모델 문제

오류 : 사전에 전달 된 모델 항목의 형식은 'System.Collections.Generic.List 1[DomainModel.Product]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable 1 [DomainModel.Entities.Product]'입니다.

모델 :

namespace DomainModel.Concrete 
{ 
    public class SqlProductsRepository : IProductsRepository 
    { 
     private Table<Product> productsTable; 

     public SqlProductsRepository(string connString) 
     { 
      productsTable = (new ProaductDataContext(connString)).GetTable<Product>(); 
     } 

     public IQueryable<Product> Products 
     { 
      get { return productsTable; } 
     } 
    } 
} 

인터페이스

namespace DomainModel.Abstract 
{ 
    public interface IProductsRepository 
    { 
     IQueryable<Product> Products { get; } 
    } 
} 

VIEW

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/ViewMaster.Master" 
     Inherits="System.Web.Mvc.ViewPage<IEnumerable<DomainModel.Entities.Product>>" %> 
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> 
Products 
</asp:Content> 
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 

    <% foreach (var product in Model) 
     { %> 
     <div class = "item"> 
     <h3> <%=product.Name%></h3> 
     <%= product.Description%> 
     <h4><%= product.Price.ToString("c")%></h4> 
     </div> 
     <%} %> 
</asp:Content> 

답변

5

응급실 너의 알 필요가있는 모든 것을 알려주는 메시지; 당신이 당신의보기를 보면

The model item passed into the dictionary is of type 
'System.Collections.Generic.List1[DomainModel.Product]', 
but this dictionary requires a model item of type 
'System.Collections.Generic.IEnumerable1[DomainModel.Entities.Product]'. 

당신은

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/ViewMaster.Master" 
     Inherits="System.Web.Mvc.ViewPage<IEnumerable<DomainModel.Entities.Product>>" %> 

Inherits="System.Web.Mvc.ViewPage<IEnumerable<DomainModel.Entities.Product>>" 당신을 일인가 무슨입니다 볼 수 있습니다. DomainModel.Product의 IEnumerable을 전달하고 있지만 다른 것을 기대하고 있습니다. 조금 더 이상 똑같은 네임 스페이스에 속한 두 개의 클래스가 있지만, 컨트롤러와 뷰의 동일한 네임 스페이스에서 동일한 클래스를 사용하는지 확인해야합니다.

그래서 나는 그런 두 제품 클래스 :

+0

당신은 내 영웅이 이유를 알아 내기 위해 노력

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/ViewMaster.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<DomainModel.Product>>" %> 

가되기 위해보기를 변경하려고 할 것입니다! 고마워, 나는 아침 내내 이걸 보냈어. 저는 교과서에있는 튜토리얼을 통해 작업하고 있으며 책에서 실수라고 생각합니다. – Susan