0

I have this project I'm working on, and I need to add paging to an audit history log. I'm not aware of if MVC comes standard with a way to add paging or if I would have to install a NuGet package. At any rate, here is the code I have so far for the audit log I need to implement paging for(I am using a repository pattern for this):

In the Interface I have this:

IEnumerable<AuditRecord> GetAuditRecords(Expression<Func<AuditRecord, bool>> filter, Func<IQueryable<AuditRecord>, IOrderedQueryable<AuditRecord>> orderBy = null, int pageIndex = 0, int pageSize = 20);

Then I have a UserHelper which has this:

public AuditInfo GetAuditInfo(SearchInfo searchInfo)
    {
        AuditInfo auditInfo = new AuditInfo();
        if (searchInfo != null)
        {
            List<AuditRecord> auditRecords =
                UserManager.GetAuditRecords(record => record.Username == searchInfo.UserName,
                                                records => records.OrderByDescending(record => record.Date), 0, 100);
            auditInfo.AuditRecords = auditRecords;
        }
        return auditInfo;
    }

Then in the controller:

public ActionResult AuditHistory(String username)
    {
        SearchInfo searchInfo = new SearchInfo { UserName = username };

        AuditInfo auditInfo = _userHelper.GetAuditInfo(searchInfo);

        return PartialView(auditInfo);
    }

And finally the view:

@if (Model != null && Model.AuditRecords != null && Model.AuditRecords.Count != 0)
{ 
<table required="False" border="0" class="data_std_results" id="tbl_std_documents">
    <thead>
        <tr>
            <th>
                <span class="resulttabletitle">Date</span>
            </th>
            <th>
                <span class="resulttabletitle">Action</span>
            </th>
            <th>
                <span class="resulttabletitle">Application</span>
            </th>
            <th>
                <span class="resulttabletitle">Modified by</span>
            </th>
            <th>
                <span class="resulttabletitle">Response</span>
            </th>
            <th>
                <span class="resulttabletitle"></span>
            </th>
        </tr>
    </thead>
    <tbody>
        @{
int index = 0;
        }
        @foreach (var item in Model.AuditRecords)
        {                
            <tr>
                <td>@item.Date.ToString()
                </td>
                <td>@item.ActionKey
                </td>
                <td>@item.ApplicationName
                </td>
                <td>@item.Delegate
                </td>
                <td>@item.Response
                </td>
                <td>
                    @if (!string.IsNullOrEmpty(item.Comment))
                    {
                        <a href="@string.Format("#comment{0}", index)" class="comment">Details</a>
                        <div class="showNone">
                            <div id="@string.Format("comment{0}", index)" class="c_gen_lb_message">
                                <h3>@string.Format("{0} {1} for {2} at {3}", item.ActionKey, item.Response, item.ApplicationName, item.Date.ToString())</h3>
                                <h5>
                                    Comments:</h5>
                                @item.Comment
                                @if (!string.IsNullOrEmpty(item.Reason))
                                {
                                    <h5>
                                        Reason:</h5>
                                    @item.Reason
                                }
                                <h5>
                                    Ip Address:</h5>
                                @item.IpAddress
                                @if (!string.IsNullOrEmpty(item.Delegate))
                                { 
                                    <h5>
                                        Modified By:</h5>
                                    @item.Delegate
                                }
                            </div>
                        </div>
                    }
                </td>
            </tr>
                    index = index + 1;
        }
    </tbody>
</table>
<div class="clearBoth">
</div>         
}
else
{
<p>
    no records found</p>
}
<script type="text/javascript">
$(document).ready(function () {

    $('#tbl_std_documents').dataTable({
        "bFilter": false,
        "bPaginate": false,
        "bSort": false,
        "bInfo": false
    });

    $("a.comment").fancybox({
        'type': 'inline',
        'transitionIn': 'elastic',
        'transitionOut': 'elastic',
        'hideOnContentClick': true,
        'speedIn': 600,
        'speedOut': 200,
        'overlayShow': false
    });

});
</script>

Any help or guidance on this would be appreciated. Thanks in advance!

1 Answer 1

3

I've used PagedList - it works very well with MVC. No need to write your own.

Sign up to request clarification or add additional context in comments.

1 Comment

That's one option I was actually looking into.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.