【问题标题】:ASP.NET MVC PaginationASP.NET MVC 分页
【发布时间】:2014-01-20 16:39:30
【问题描述】:

我正在处理这个项目,我需要将分页添加到审核历史记录日志中。我不知道 MVC 是否带有添加分页的标准方式,或者我是否必须安装 NuGet 包。无论如何,这是我到目前为止需要实现分页的审计日志的代码(我为此使用了存储库模式):

在界面中我有这个:

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

然后我有一个 UserHelper 有这个:

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

然后在控制器中:

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

        AuditInfo auditInfo = _userHelper.GetAuditInfo(searchInfo);

        return PartialView(auditInfo);
    }

最后是视图:

@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>

对此的任何帮助或指导将不胜感激。提前致谢!

【问题讨论】:

    标签: asp.net-mvc pagination


    【解决方案1】:

    我使用过PagedList - 它与 MVC 配合得非常好。无需自己编写。

    【讨论】:

    • 这是我实际上正在研究的一个选项。
    猜你喜欢
    • 2012-10-16
    • 1970-01-01
    • 2012-05-03
    • 2023-03-10
    • 1970-01-01
    • 2019-07-23
    • 2012-06-21
    相关资源
    最近更新 更多