简体   繁体   English

在Asp.net MVC中的分页4/5

[英]Pagination in Asp.net MVC 4 / 5

I am sending a list of records by using model object to the view and now i want paging but the problem is model object what i am passing is not a pagedlist object. 我正在通过使用模型对象向视图发送记录列表,现在我要分页,但问题是我传递的模型对象不是pagedlist对象。

how to convert simple object to pagedList object 如何将简单对象转换为pagedList对象

here is my code 这是我的代码

public ActionResult ApplicantsRecord(int page = 1)
{
    List<ApplicantsRecord> ar = new List<ApplicantsRecord>();
    ApplicantsRecord a = new ApplicantsRecord();
    List<ApplicantsRecordDetailViewModel> apvmlist = 
                                    new List<ApplicantsRecordDetailViewModel>();
    ApplicantsRecordDetailViewModel apvm = new ApplicantsRecordDetailViewModel();
    //ar = db.ApplicantsRecords.ToList();
    var groupedAR = db.ApplicantsRecords
                      .GroupBy(x => x.SessionId)
                      .Select(y => new
                      {
                          SessionId = y.Key,
                          ApplicationsRecords = y.FirstOrDefault(),
                      }).OrderByDescending(x => x.ApplicationsRecords.LoginDate)
                      .ToPagedList(page, 10);

    foreach (var i in groupedAR)
    {
        ar.Add(i.ApplicationsRecords);
    }

    return View(ar);
}

If I understood your problem, you probably want to do something like this: 如果我了解您的问题,则可能需要执行以下操作:

var groupedAR = db.ApplicantsRecords.GroupBy(x => x.SessionId)
    .Select(y => new
    {
        SessionId = y.Key,
        ApplicationsRecords = y.FirstOrDefault(),

    }).OrderByDescending(x => x.ApplicationsRecords.LoginDate).Select(i=>i.ApplicationsRecords).ToPagedList(page, 10);

return View(ar);

Note .Select(i=>i.ApplicationsRecords) in above 注意。在上面选择.Select(i=>i.ApplicationsRecords)


Or you can do something like this: 或者,您可以执行以下操作:

var groupedAR = db.ApplicantsRecords.GroupBy(x => x.SessionId)
    .Select(y => new
    {
        SessionId = y.Key,
        ApplicationsRecords = y.FirstOrDefault(),

    }).OrderByDescending(x => x.ApplicationsRecords.LoginDate);

foreach (var i in groupedAR)
{
    ar.Add(i.ApplicationsRecords);
}
return View(ar.ToPagedList(page, 10));

Note that moved ToPagedList in other place 请注意,将ToPagedList移至其他位置

Use jQuery DataTables https://datatables.net/ . 使用jQuery DataTables https://datatables.net/ by default it has a paging 默认情况下它有一个分页

View 视图

var oProtocolTable = $('#MyTable').DataTable({
        "ajax": "/Home/ProtocolAjaxHandler",
    });

Controller 控制者

public ActionResult ProtocolAjaxHandler(JQueryDataTableParamModel param)
        {
            var ar = db.ApplicantsRecords.ToList();
            if (ar == null)
                return Json(new
                {
                    data = new List<string>()
                },
                JsonRequestBehavior.AllowGet);
            return Json(new
            {
                aaData = from emp in riski
                         select new[] { emp.col1, emp.col2 }

            },
            JsonRequestBehavior.AllowGet);

        }

Instead of using a List<ApplicantsRecord> as the model in your view, use PagedList.IPagedList<ApplicantsRecord> : 不要在您的视图中使用List<ApplicantsRecord>作为模型,而应使用PagedList.IPagedList<ApplicantsRecord>

( top of ApplicantsRecord.cshtml ) ApplicantsRecord.cshtml的顶部

@model PagedList.IPagedList<ApplicantsRecord>

Then just return your paged list from your controller without copying it into something else: 然后,只需从控制器返回分页列表即可,而不将其复制到其他内容中:

( end of ApplicantsRecord() controller action ) ApplicantsRecord()控制器操作结束

return View(groupedAr);

Once you do that, you should be able to access your model as you are already using it, but it should now have the paging values that you need: 完成此操作后,您应该已经可以使用模型,但是现在它应该具有所需的分页值:

( somewhere in ApplicantsRecord.cshtml ) 在ApplicantsRecord.cshtml中的某处

Page @Model.PageNumber of @Model.PageCount
<input type="hidden" name="page" value="@Model.PageNumber" />

You could try using Webdiyer.MvcPager. 您可以尝试使用Webdiyer.MvcPager。 MvcPager is a free and open source paging component for ASP.NET MVC, it expose a series of extension methods for using in ASP.NET MVC applications, the implementation was inspired by Scott Guthrie's PagedList idea, it was first published in 2009 and had been steadily improved and updated since then. MvcPager是ASP.NET MVC的免费开放源代码分页组件,它公开了在ASP.NET MVC应用程序中使用的一系列扩展方法,该实现的灵感来自Scott Guthrie的PagedList想法,该想法于2009年首次发布,并且已经从那以后稳步改进和更新。 The latest version 3.0 is one the most powerful ASP.NET MVC paging controls.View online demos at http://en.webdiyer.com/mvcpager/demos/ 最新版本3.0是功能最强大的ASP.NET MVC分页控件之一。请访问http://en.webdiyer.com/mvcpager/demos/查看在线演示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM