John John
John John

Reputation: 1

How can I apply paging to a non-list type

I have the following action method:-

public ActionResult Index(string searchTerm=null, int page = 1)
{
            var racks = repository.AllFindRacks(searchTerm).OrderBy(a=>a.Technology.Tag).ToPagedList(page, 5) ;

            if (Request.IsAjaxRequest())
            {

                return PartialView("_RackTable", racks);
            }
            return View(racks);
}

which calls the following repository method:-

public RackJoinList AllFindRacks(string q)
{
            RackJoinList rjlist = new RackJoinList();
            var racks = from rack in tms.TMSRacks.Where(a => a.Technology.Tag.ToUpper().StartsWith(q.ToUpper()) || (q == null))

                  select rack;

            rjlist.Racks = racks.ToList();
            var resources = from resource in entities.Resources
                            join c in rjlist.Racks
                            on resource.RESOURCEID equals c.Technology.IT360ID
                            select resource;
            rjlist.Resources = resources;

            return rjlist;
}

and the RackJoinList view model is :-

public class RackJoinList
{
        public IEnumerable<TMSRack> Racks { get; set; }
        public IEnumerable<Resource> Resources { get; set; }
}

but as the repository method return single object, I am unable to apply orderby and paging to it. Can anyone advice on how to solve this?

Upvotes: 1

Views: 68

Answers (1)

Shaz
Shaz

Reputation: 1396

Just a shot in the dark but what about this:

RackJoinList list = repository.AllFindRacks(searchTerm);
list.Racks = list.Racks.OrderBy(a=>a.Technology.Tag).ToPagedList(page, 5);

AllFindRacks returns one object, but that one object has two IEnumerable collections in it, Racks and Resources. I'm assuming you want to call OrderBy on Racks here.

Edit Explanation:

What I did was this;

Store the RackJoinList object, because this will be sent off elsewhere later.

Alter the RackJoinList object's Racks collection.

Send off the RackJoinList object with the altered Racks collection.

Caveat: I'm unsure how ToPagedList works. Is there documentation somewhere on it? I tried Google to no avail.

Edit #2:

Here's how I would restructure it based on the source of PagedList. The caveat here is that I can't compile this so I don't know if PagedList<TMSRack> is the right type. Let this be known as a case where writing documentation using var is annoying!

public ActionResult Index(string searchTerm=null, int page = 1)
{
    RackJoinList list = repository.AllFindRacks(searchTerm);
    list.PagedRacks = list.Racks.OrderBy(a=>a.Technology.Tag).ToPagedList(page, 5);

    if (Request.IsAjaxRequest())
    {
        return PartialView("_RackTable", racks);
    }

    return View(list);
}

public class RackJoinList
{
    public IEnumerable<TMSRack> Racks { get; set; }
    public IEnumerable<Resource> Resources { get; set; }
    public PagedList<TMSRack> PagedRacks { get; set; }
}

Upvotes: 1

Related Questions