asp.net mvc返回视图pagedlist

时间:2015-05-21 15:05:37

标签: c# asp.net-mvc pagedlist

我有以下行动结果

 public ActionResult TheMostClickedEmailReport(int ID, int gonderim,int? page)
        {
            int pageIndex = page ?? 1;
            int pagesize = 10;

            MailingClickService mailingclickservice = new MailingClickService();
            TemplateLinkService tmpservice = new TemplateLinkService();
            int FirmID = (int)Session["FirmID"];
            var linkler = mailingclickservice.GetByCriteria(x => x.MailingID == ID && x.Gonderim == gonderim && x.Mailing.FirmUser.FirmID == FirmID).IslemSonucu.GroupBy(x => x.MailingEmail).ToPagedList(pageIndex, pagesize);



            ViewBag.gonderim = gonderim;
            ViewBag.MailingID = ID;

            List<EmailLinkClickReport> emailcliklistreport = new List<EmailLinkClickReport>();
            if (linkler.Count() > 0)
            {

                foreach (var item in linkler)
                {
                    EmailLinkClickReport emaillinkclick = new EmailLinkClickReport();

                    emaillinkclick.Key = item.Key;
                    emaillinkclick.TotalClicksCount = item.Select(x => x.MailingEmail).Count();
                    emailcliklistreport.Add(emaillinkclick);
                }


                return View(emailcliklistreport);
            }
            else
            {
                return RedirectToAction("MailingReport", "Report", new { id = ID, gonderim = gonderim });
            }
        }

linkler 有50个数据。我使用 foreach(链接器中的项目)

linkler 中选择10个数据

我想在v ar linkler = mailingclickservice.GetByCriteria(x => x.MailingID == ID && x.Gonderim == gonderim && x.Mailing.FirmUser.FirmID == FirmID).IslemSonucu.GroupBy(x => x.MailingEmail).ToPagedList(pageIndex, pagesize);中选择10个数据 部分

但是我不知道如何通过返回视图将链接器转换为IPagedList?

如果我返回以下代码

return View(emailcliklistreport);

如下所示

return View(emailcliklistreport.ToPagedList(pageIndex,pagesize);

这对我来说不合逻辑,因为我两次返回列表

您有什么建议并建议我更改我的代码。

如何通过强制转换ToPagedList来返回链接器以进行查看?

1 个答案:

答案 0 :(得分:1)

ToPagedList应该是您在将数据集返回到视图之前对数据集执行的最后一个操作。就目前而言,您完全放弃了PagedList,只是将List<EmailLinkClickReport>返回到视图。

另外,对于什么是值得的,无论如何都不需要foreach循环,因为你可以使用LINQ的Select方法来转换数据集。尝试:

var linkler = mailingclickservice
    .GetByCriteria(x =>
        x.MailingID == ID &&
        x.Gonderim == gonderim &&
        x.Mailing.FirmUser.FirmID == FirmID)
    .IslemSonucu.GroupBy(x => x.MailingEmail)
    .Select(x => new EmailLinkClickReport
        {
            Key = x.Key;
            TotalClicksCount = x.Count();
        })
    .ToPagedList(pageIndex, pagesize);

我添加了宽松的间距,使其更容易阅读。唯一的新部分是Select电话。同时从Select(x => x.MailingEmail)移除了TotalClickCounts爵士乐,因为它只会增加额外的处理时间。