Asp.net mvc ASP.NETMVC模型问题

Asp.net mvc ASP.NETMVC模型问题,asp.net-mvc,anonymous-types,Asp.net Mvc,Anonymous Types,你能帮我了解一下一般收集的问题吗?提前谢谢 错误:传入字典的模型项的类型为“System.Collections.Generic.List1[DomainModel.Product]”,但此字典需要类型为“System.Collections.Generic.IEnumerable1[DomainModel.Entities.Product]”的模型项。 namespace DomainModel.Concrete { public class SqlProductsRepository

你能帮我了解一下一般收集的问题吗?提前谢谢

错误:传入字典的模型项的类型为“System.Collections.Generic.List
1[DomainModel.Product]”,但此字典需要类型为“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.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.Concrete
{
公共类SqlProductsRepository:IPProductsRepository
{
私有表产品稳定;
公共SqlProductsRepository(字符串connString)
{
productsTable=(新的ProaductDataContext(connString)).GetTable();
}
公共卫生产品
{
获取{return productsTable;}
}
}
}
接口

namespace DomainModel.Abstract
{
    public interface IProductsRepository
    {
        IQueryable<Product> Products { get; }
    }
}
namespace DomainModel.Abstract
{
公共接口IProductsRepository
{
IQueryable产品{get;}
}
}
查看

<%@ 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>

产品

错误消息告诉您所有需要知道的信息

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”
让您大吃一惊。您传递的是DomainModel.Product的IEnumerable,但您期望的是其他内容。有点奇怪的是,在同一名称空间中有两个名为相同的类,但没关系,您需要确保在控制器和视图的同一名称空间中使用相同的类

所以我会试着改变你的观点

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


然后试着找出为什么您有两个产品类:)

错误消息告诉您需要知道的所有信息

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”
让您大吃一惊。您传递的是DomainModel.Product的IEnumerable,但您期望的是其他内容。有点奇怪的是,在同一名称空间中有两个名为相同的类,但没关系,您需要确保在控制器和视图的同一名称空间中使用相同的类

所以我会试着改变你的观点

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


然后试着找出为什么你有两个产品类别:)

你是我的英雄!!谢谢,我整个上午都在忙这个。我正在阅读一本教科书中的教程,这似乎是书中的一个错误。你是我的英雄!!谢谢,我整个上午都在忙这个。我正在阅读一本教科书中的教程,这似乎是书中的一个错误。