这是C#MVC的完整入门者,需要一些帮助。
我被困在将viewmodel数据传递给PagedList。
我究竟做错了什么?
我得到的控制器中的错误消息是:


  无法将类型'PagedList.IPagedList'隐式转换为'PagedList.IPagedList'。存在显式转换(您是否缺少演员表?)


我已经检查了很多与此主题相关的其他帖子,但无法解决我的问题。

这是我的视图模型:

public partial class NaloziViewModel
{
    public int IDNalozi { get; set; }
    public int KlijentID { get; set; }

    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy HH:mm}", ApplyFormatInEditMode = true)]
    public DateTime VremeStart { get; set; }

    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy HH:mm}", ApplyFormatInEditMode = true)]
    public DateTime? VremeKraj { get; set; }

    public int KilometrazaStart { get; set; }
    public int? KilometrazaKraj { get; set; }
    public virtual Klijent Klijent { get; set; }
    public int? Page { get; set; }
}


分页类

public class Pagedclass
{
    public PagedList.IPagedList<NaloziViewModel> mylist { get; set; }
    public int KilometrazaSum { get; set; }
    public int VremeSum { get; set; }
}


控制器:

public ActionResult Index(int? page, DateTime? dateFrom, DateTime? dateTo, string klijentIme)
{
    int pageSize = 10;
    int pageNumber = (page ?? 1);
    var naloziFilter = db.Nalozis.Include(n => n.Klijent).OrderByDescending(n => n.IDNalozi).Where(n => n.Obrisan == 0).ToPagedList(pageNumber,pageSize);

    if (dateFrom != null && dateTo != null )
    {
        naloziFilter = naloziFilter.Where(n => n.Klijent.NazivKlijenta.Contains(klijentIme)).Where(n => n.VremeStart >= dateFrom).Where(n => n.VremeKraj <= dateTo).ToPagedList(pageNumber,pageSize);
        TimeSpan vremeSum = new TimeSpan();
        naloziFilter.Where(x => x.VremeKraj.HasValue).ToList().ForEach(x => vremeSum += x.VremeKraj.Value - x.VremeStart);

        var model = new Pagedclass
        {
            mylist = naloziFilter,
            KilometrazaSum = naloziFilter.Sum(n => n.KilometrazaKraj ?? 0) - naloziFilter.Sum(n => n.KilometrazaStart),
            VremeSum = (vremeSum.Days*24)+vremeSum.Hours,

        };
        return View(model);
    }
    else
    {
        TimeSpan vremeSum = new TimeSpan();
        naloziFilter.OrderByDescending(n => n.IDNalozi).Where(n => n.Obrisan == 0).Where(x => x.VremeKraj.HasValue).ToList().ForEach(x => vremeSum += x.VremeKraj.Value - x.VremeStart);

        var model = new Pagedclass
        {
            mylist = naloziFilter,
            KilometrazaSum = naloziFilter.Sum(n => n.KilometrazaKraj ?? 0) - naloziFilter.Sum(n => n.KilometrazaStart),
            VremeSum = (vremeSum.Days * 24) + vremeSum.Hours
        };
        return View(model);
    }
}


我的看法:

@model Pagedclass

@using PagedList;

@using PagedList.Mvc;

....

最佳答案

基本版本(不使用自动映射器)是更改Linq以返回所需的对象类型(通过在select子句中对其进行更新):

例如改变这个:

naloziFilter.OrderByDescending(n => n.IDNalozi)
      .Where(n => n.Obrisan == 0)
      .Where(x => x.VremeKraj.HasValue).ToList().ForEach(x => vremeSum += x.VremeKraj.Value - x.VremeStart);


像这样:

naloziFilter.OrderByDescending(n => n.IDNalozi)
      .Where(n => n.Obrisan == 0)
      .Where(x => x.VremeKraj.HasValue)
      .Select(x=> new NaloziViewModel(){
          IDNalozi = x.IDNalozi,
          KlijentID = x.KlijentID,
          VremeStart = x.VremeStart,
          VremeKraj = x.VremeKraj,
          KilometrazaStart = x.KilometrazaStart,
          KilometrazaKraj = x.KilometrazaKraj,
          Klijent = x.Klijent,
          Page = x.Page
       })
      .ToList().ForEach(x => vremeSum += x.VremeKraj.Value - x.VremeStart);

关于c# - 如何将ViewModel传递给PagedList,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26596648/

10-10 16:17