Mihai Tibrea
Mihai Tibrea

Reputation: 641

How to use pageList in a view

I am trying to use PagedList to obtain a paged list of some tickets.

The model i am using is the following :

public class TicketIndexModel
{
    public IEnumerable<Ticket> ticketList { get; set; }
    public string TicketTitle { get; set; }
    public string TicketDescription { get; set; }
}

and in the controller i am sending to the view :

TicketIndexModel indexTicketList = new TicketIndexModel();
indexTicketList.ticketList = db.Tickets.Where(r => r.UserName == currentUserName).ToList().OrderByDescending(r => r.DateCreated);
return View(indexTicketList.ticketList.ToPagedList(pageNumber,pageSize));

In the view i have 2 things, a list of all the tickets and a simple form to add another ticket. Here is a little code :

@model PagedList.IPagedList<CDS.Models.TicketIndexModel>
@foreach (var row in Model)
{
    foreach(var item in row.ticketList){
// the list of tickets
}
}

But my problem is in the form for adding a new ticket

@Html.LabelFor(model => model.TicketTitle) 

model.TicketTitle is not available to access.

How can i fix this ? Or do you suggest i use a different paging system ?

EDIT :

I made the changes you said in the controller, switched to a TicketIndexModel in the view, changed my model to use an IPagedList.

I also added the following code : Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount

@if (Model.HasPreviousPage)
{
    @Html.ActionLink("<<", "Index", new { page = 1, sortOrder = ViewBag.CurrentSort, currentFilter=ViewBag.CurrentFilter  })
    @Html.Raw(" ");
    @Html.ActionLink("< Prev", "Index", new { page = Model.PageNumber - 1, sortOrder = ViewBag.CurrentSort, currentFilter=ViewBag.CurrentFilter  })
}
else
{
    @:<<
    @Html.Raw(" ");
    @:< Prev
}

@if (Model.HasNextPage)
{
    @Html.ActionLink("Next >", "Index", new { page = Model.PageNumber + 1, sortOrder = ViewBag.CurrentSort, currentFilter=ViewBag.CurrentFilter  })
    @Html.Raw(" ");
    @Html.ActionLink(">>", "Index", new { page = Model.PageCount, sortOrder = ViewBag.CurrentSort, currentFilter=ViewBag.CurrentFilter  })
}
else
{
    @:Next >
    @Html.Raw(" ")
    @:>>
}

But now i get this error
Models.TicketIndexModel' does not contain a definition for 'PageCount' and no extension method 'PageCount' accepting a first argument of type 'Models.TicketIndexModel' could be found (are you missing a using directive or an assembly reference?)

Should i change the model ? I don't know what to do exactly, it's the first time i use this, and i only started using MVC 3 months ago.

Upvotes: 0

Views: 2540

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038780

In the view i have 2 things, a list of all the tickets and a simple form to add another ticket.

Then IPagedList<CDS.Models.TicketIndexModel> is not a suitable view model for this view since you need more information in it than just a simple paged list of tickets.

You could make this view strongly typed to the TicketIndexModel type instead:

TicketIndexModel indexTicketList = new TicketIndexModel();
indexTicketList.ticketList = db
    .Tickets
    .Where(r => r.UserName == currentUserName)
    .ToList()
    .OrderByDescending(r => r.DateCreated)
    .ToPagedList(pageNumber,pageSize);
return View(indexTicketList);

and your view:

@model TicketIndexModel
...
Here you can access Model.TicketTitle since you need it and Model.ticketList to show your list

Of course you might need to adapt the model so that it uses an IPagedList<Ticket> instead of IEnumerable<Ticket>.

Upvotes: 1

Related Questions