asp.net mvc 2 - 无法将类型"System.Collections.Generic.List"隐式转换为"Models.CustomerViewModel"



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

我的界面由组成

public interface ICustomersAndSitesRepository
{
    IQueryable<CustomerSite> CustomerSites { get; }
    IQueryable<Customer> Customers { get; }
    IQueryable<ICustomersAndSitesRepository> CustomerAndSites { get; }
}

然后我的存储库我有这个方法

public IQueryable <ICustomersAndSitesRepository> CustomerAndSites
    {
        get { return CustomerAndSites; }
    }

然后我有我的视图模型

public class CustomerSitesListViewModel
{
    public IList<CustomerSite> CustomerSites { get; set; }
    public PagingInfo PagingInfo { get; set; }
    public CustomerViewModel Customers { get; set; }
}

我的控制器动作

public ViewResult List([DefaultValue(1)] int page)
{
    var customersWithSitesToShow = customersAndSitesRepository.CustomerAndSites;
    var viewModel = new CustomerSitesListViewModel
    {                
        Customers = customersWithSitesToShow.Skip((page - 1) * PageSize).Take(PageSize).ToList(),
        PagingInfo = new PagingInfo
        {
            CurrentPage = page,
            ItemsPerPage = PageSize,
            TotalItems = customersWithSitesToShow.Count()
        }
    };
    return View(viewModel);  //Passed to view as ViewData.Model (or simply model)
}

当我试图将集合传递给期望列表的分页函数时,这一行抛出了一个错误。

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

错误为

 Cannot implicitly convert type 'System.Collections.Generic.List to 'Models.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; }
}

如错误所示:属性CustomerSitesListViewModel.Customers的类型为CustomerViewModel,但您正试图分配一个List<CustomerSite>

你的意思是:

CustomerSites = customersWithSitesToShow
                  .Skip((page - 1) * PageSize)
                  .Take(PageSize)
                  .ToList()

或者这个:

Customers = new CustomerViewModel(customersWithSitesToShow...),

相关内容

  • 没有找到相关文章

最新更新