无法将类型“System.Collections.Generic.List”隐式转换为“Models.CustomerViewModel”

发布于 2024-11-18 05:00:53 字数 2585 浏览 3 评论 0原文

我正在尝试传递包含自定义界面中包含的两个对象的数据列表,

我的界面包含

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; }
}

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 { 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; }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终止放荡 2024-11-25 05:00:53

正如错误所述:属性 CustomerSitesListViewModel.Customers 的类型为 CustomerViewModel,但您试图分配 List

你的意思是这样的:

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

或者也许是这样的:

Customers = new CustomerViewModel(customersWithSitesToShow...),

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...),
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文