Asp.net mvc 2 无法隐式转换类型';System.Collections.Generic.List到';Models.CustomerViewModel';

Asp.net mvc 2 无法隐式转换类型';System.Collections.Generic.List到';Models.CustomerViewModel';,asp.net-mvc-2,repository,viewmodel,ilist,icollection,Asp.net Mvc 2,Repository,Viewmodel,Ilist,Icollection,我试图传递一个包含自定义接口中包含的两个对象的数据列表 我的界面包括 public interface ICustomersAndSitesRepository { IQueryable<CustomerSite> CustomerSites { get; } IQueryable<Customer> Customers { get; } IQueryable<ICustomersAndSitesRepository> Customer

我试图传递一个包含自定义接口中包含的两个对象的数据列表

我的界面包括

public interface ICustomersAndSitesRepository
{
    IQueryable<CustomerSite> CustomerSites { get; }
    IQueryable<Customer> Customers { get; }
    IQueryable<ICustomersAndSitesRepository> CustomerAndSites { get; }
}
当我试图将集合传递给需要列表的分页函数时,此行抛出一个错误

    Customers = customersWithSitesToShow.Skip((page - 1) * PageSize).Take(PageSize).ToList(),
错误是

 Cannot implicitly convert type 'System.Collections.Generic.List to 'Models.CustomerViewModel'
是否有方法转换返回的列表,以便在viewmodel中使用

这是客户视图模型

public class CustomerViewModel
{
    public int Id { get; set; }
    public string CustomerName { get; set; }
    public string PrimaryContactName { get; set; }
    public string PrimaryContactNo { get; set; }
    public string PrimaryEmailAddress { get; set; }
    public string SecondaryContactName { get; set; }
    public string SecondaryContactNo { get; set; }
    public string SecondaryEmailAddress { get; set; }
    public DateTime RegisteredDate { get; set; }
    public string WasteCarrierRef { get; set; }
    public string UnitNo { get; set; }
    public string StreetName { get; set; }
    public string Town { get; set; }
    public string County { get; set; }
    public string Postcode { get; set; }
    public byte[] ImageData { get; set; }
    public string ImageMimeType { get; set; }
    public SiteViewModel Site { get; set; }
}

如错误所示:属性
CustomerSitesListViewModel.Customers
的类型为
CustomerServiceWModel
,但您正在尝试分配一个
列表

你是说:

CustomerSites = customersWithSitesToShow
                  .Skip((page - 1) * PageSize)
                  .Take(PageSize)
                  .ToList()
或者可能是这样:

Customers = new CustomerViewModel(customersWithSitesToShow...),

哪一行抛出错误?如果是“return View(viewModel);”,那么您能提供正在调用的视图中的标记吗?@bzarah,谢谢,我更新了问题以显示导致错误的行,这是我试图将集合传递给分页函数的地方。变量“Customers”是否声明为“Models.CustomerViewModel”类型?一定是吧?那将是最好的选择issue@Liam您应该向我们展示您的
CustomerViewModel
类。仅凭名称,我猜您正在尝试将
CustomerViewModel
的实例设置为列表,这显然是不等价的types@bzarah,是,它在viewmodel中定义为公共CustomerServiceModel客户{get;set;}嗨,我正在使用viewmodel试图访问这两个对象的属性,无论是客户还是客户站点,我都更新了问题以显示我的客户视图模型。我无法找到从一个视图/显示模板访问2个对象的其他方法。再次感谢,当使用第二个建议时,它会抛出一个错误,指出customerview模型不包含带1个参数的构造函数。请忽略第二个建议(我在发布CustomerViewModel之前提出,这只是猜测)
public class CustomerViewModel
{
    public int Id { get; set; }
    public string CustomerName { get; set; }
    public string PrimaryContactName { get; set; }
    public string PrimaryContactNo { get; set; }
    public string PrimaryEmailAddress { get; set; }
    public string SecondaryContactName { get; set; }
    public string SecondaryContactNo { get; set; }
    public string SecondaryEmailAddress { get; set; }
    public DateTime RegisteredDate { get; set; }
    public string WasteCarrierRef { get; set; }
    public string UnitNo { get; set; }
    public string StreetName { get; set; }
    public string Town { get; set; }
    public string County { get; set; }
    public string Postcode { get; set; }
    public byte[] ImageData { get; set; }
    public string ImageMimeType { get; set; }
    public SiteViewModel Site { get; set; }
}
CustomerSites = customersWithSitesToShow
                  .Skip((page - 1) * PageSize)
                  .Take(PageSize)
                  .ToList()
Customers = new CustomerViewModel(customersWithSitesToShow...),