Cannot implicitly convert type 'System.Collections.Generic.List to 'Models.CustomerViewModel'
I am trying to pass a list of data containing two objects that are contained in a custom interface,
My interface consists of
public interface ICustomersAndSitesRepository
{
IQueryable<CustomerSite> CustomerSites { get; }
IQueryable<Customer> Customers { get; }
IQueryable<ICustomersAndSitesRepository> CustomerAndSites { get; }
}
Then my repository i have this method
public IQueryable <ICustomersAndSitesRepository> CustomerAndSites
{
get { return CustomerAndSites; }
}
Then i have my viewmodel
public class CustomerSitesListViewModel
{
public IList<CustomerSite> CustomerSites { get; set; }
public PagingInfo PagingInfo { get; set; }
public CustomerViewModel Customers { get; set; }
}
And my controller action
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)
}
This line throws an error as im trying to pass the collection to my paging function thats expecting a list.
Customers = customersWithSitesToShow.Skip((page - 1) * PageSize).Take(PageSize).ToList(),
The error is
Cannot implicitly convert type 'System.Collections.Generic.List to 'Models.CustomerViewModel'
Is there a way to convert the list that is being returned so that it can be used in the viewmodel?
This is the customer view model
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 { 开发者_StackOverflow社区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; }
}
As the error says: the property CustomerSitesListViewModel.Customers
is of type CustomerViewModel
, but you are trying to assign a List<CustomerSite>
.
Did you mean this instead:
CustomerSites = customersWithSitesToShow
.Skip((page - 1) * PageSize)
.Take(PageSize)
.ToList()
or maybe this:
Customers = new CustomerViewModel(customersWithSitesToShow...),
精彩评论