I have a 'Ticket' View with a partial View of "Comments". A Ticket can have many comments. I want to create a partial view of Comments inside the Ticket view so that I can add paging functionality to the Comments without impacting the ticket view. (I'll have 2 other partials with same functionality pertaining to different data but that are associated with the Ticket) I'm attempting to use the PagedList library available via Nuget.
In theory, I pass the ticket model to the GetComments controller where I search ticket comments comparing the ticketId's. I return a ToPagedList of Comments to the partial view. I want the paging to work within the partial view and with the GetComments controller. (I'd like to leave the 'Details' controller of Tickets with 'out of the box' functionality. e.g. return View(db.tickets.find(id))
I've never implemented a partial before, this is my 4th week learning MVC so any help is appreciated!
Parent View: TicketDetails.cshtml
@model DearSanta.Models.Ticket
//ticket display markup removed to simplify
// Where I wan my partial view of comments
<div class="col-md-4">
@Html.Partial("GetComments", Model)
</div>
Partial View: GetComments.cshtml
@model IPagedList<DearSanta.Models.TicketComment>
@using PagedList;
@section CSS{
<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css">
}
<div id="comments">
<h4 class="title">Ticket Comments</h4>
<ul class="timeline">
@foreach (var c in Model)
{
<li>
<i class="fa fa-comment"></i>
<span class="date">@c.CreateDate.ToString("d MMM")</span>
<div class="content">
<p><strong>@c.User.DisplayName</strong> @c.Comment</p>
<small>@Convert.ToInt32(((DateTime.Now - c.CreateDate).TotalDays)) days ago</small>
</div>
</li>
}
</ul>
<div>
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
@Html.PagedListPager(Model, page => Url.Action("GetComments","Tickets", new { page }))
</div>
</div>
Partial View Controller:
//Controller references: using PagedList;
using PagedList.Mvc;
public PartialViewResult GetComments(Ticket model, int page = 1)
{
int pageSize = 3;
var comments = db.TicketComment.Where(c => c.TicketId == model.TicketId).ToPagedList(page, pageSize);
return PartialView("GetComments", comments);
}