3

Here comes a complete beginner in C# MVC that needs some help. I am stuck in passing viewmodel data to a PagedList. What am I doing wrong? Error message in the controller that I get is:

Cannot implicitly convert type 'PagedList.IPagedList' to 'PagedList.IPagedList'. An explicit conversion exists(are you missing a cast?)

I've checked a ton of other posts on this subject but was not able to solve my problem.

Here is my viewmodel:

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

Pagedclass

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

Controller:

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

My View:

@model Pagedclass

@using PagedList;

@using PagedList.Mvc;

....
ekad
  • 14,436
  • 26
  • 44
  • 46
  • 2
    The collection you get back from the query on `db.Nalozis` is a collection of `Nalozi` not `NaloziViewModel`. You'll need to project these items to `NaloziViewModel`. – Jasen Oct 27 '14 at 21:17
  • @Jasen Can you please help me with that. Do not know how that should be done. – Vladimir Djordjevic Oct 28 '14 at 08:24
  • The [answer to this question](http://stackoverflow.com/a/5392180/2030565) has an example of a projection. The question asks about [AutoMapper](https://github.com/AutoMapper/AutoMapper/wiki/Getting-started) which is a tool to help you with these projections so you don't need to type these out repeatedly. – Jasen Oct 28 '14 at 17:46

1 Answers1

0

The basic version, without using an automapper, is to change your Linq to return the desired object type (by newing it in a select clause):

e.g. change this:

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

to somthing like this:

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);
iCollect.it Ltd
  • 92,391
  • 25
  • 181
  • 202