简体   繁体   English

分页在剃须刀中只有一个局部视图

[英]Pagination for only one partial view in razor

I have viewmodel with 3 classes 我有3个类的viewmodel

    public IEnumerable<Nazivi_grupa> ngrupa { get; set; }
    public IEnumerable<Grupe_radova> gradova { get; set; }
    public IEnumerable<Pozicije> pozicije { get; set; }

partial view partPozicijeView.cshtml 部分视图partPozicijeView.cshtml

@model PagedList.IPagedList<WebApplication3.ViewModels.mainViewModel>
@using PagedList.Mvc;

@{ some code to show table }

and main view index.cshtml 和主视图index.cshtml

@model WebApplication3.ViewModels.mainViewModel
@using PagedList.Mvc;
    <tr>
        <td>Category&nbsp;</td>
        <td>&nbsp;:</td>
        <td><div id="KnjigeNormativa">@Html.Partial("partKnjigeNormativaView", Model)</div> </td>
    </tr>
    <tr>
        <td>Sub - Category&nbsp;</td>
        <td>&nbsp;:</td>
        <td><div id="GrupeRadova"> @Html.Partial("partGrupeRadovaView", Model)</div></td>
    </tr>
    <tr>
        <td>Products&nbsp;</td>
        <td>&nbsp;:</td>
        <td><div id="Pozicije"> @Html.Partial("partPozicijeView", Model)</div></td>
    </tr>

that calls 3 partial views to show first 2 tables in cascaded dropdowns and 3rd one (pozicije) in a table. 会调用3个局部视图,以级联下拉菜单显示前2个表,并在表中显示第3个(pozicije)表。

Now i'm trying to add pagination to that table, but no matter what, i'm getting an error 现在我正在尝试向该表添加分页,但是无论如何,我都遇到了错误

The model item passed into the dictionary is of type 'WebApplication3.ViewModels.mainViewModel', but this dictionary requires a model item of type 'PagedList.IPagedList`1[WebApplication3.ViewModels.mainViewModel]'.

on 3rd partial view call. 在第3次局部视图调用中。

Here's also code for controller 这也是控制器的代码

    public ActionResult SelectPozicije(string SelectedPosId, int? pnum)
    {


        int pageNumber = (pnum ?? 1);

        mainViewModel mv = new mainViewModel();
        mv.pozicije = new List<Pozicije>();

//            mv.pozicije = (from p in mainViewModel.getPozicije()
//                           where p.grupa == SelectedPosId
//                           select p).ToPagedList(pageNumber, 25);
        mv.pozicije = (from p in mainViewModel.getPozicije()
                       where p.grupa == SelectedPosId
                       select p).ToList();


        return PartialView("partPozicijeView", mv);
    }

I tried (commented code) to return .ToPagedList() but no success... 我试图(注释代码)返回.ToPagedList()但没有成功...

can someone point me how to do this? 有人可以指出我该怎么做吗?

Try this: 尝试这个:

public ActionResult SelectPozicije(string SelectedPosId, int? pnum)
{
    int pageNumber = (pnum ?? 1);

    mainViewModel mv = new mainViewModel();
    mv.pozicije = new List<Pozicije>();
    mv.pozicije = (from p in mainViewModel.getPozicije()
                   where p.grupa == SelectedPosId
                   select p).ToList();

   if (mv.pozicije.Count() != 0)
   {
      var resultsPage = mv.pozicije.ToPagedList(pageNumber, 20);
      ViewBag.ResultsPage = resultsPage;
      return PartialView("partPozicijeView", mv);
   }
   else
   {
      return PartialView("partPozicijeView");
   }
}

Then in the view: 然后在视图中:

@Html.PagedListPager((IPagedList)ViewBag.ResultsPage,
                      page => Url.Action("SelectPozicije", "ControllerName", new { SelectedPosId = YourPosId, pnum = page}, 
                      PagedListRenderOptions.PageNumbersOnly)

Modify what needs to be modified accordingly. 修改需要相应修改的内容。 Hope it helps. 希望能帮助到你。

try this code 试试这个代码

//your main model
namespace WebApplication3.ViewModels
{
public class mainViewModel
{
    public IEnumerable<Nazivi_grupa> ngrupa { get; set; }
    public IEnumerable<Grupe_radova> gradova { get; set; }
    public IEnumerable<Pozicije> pozicije { get; set; }
}
}

your main view 您的主要观点

//your main view Index.cshtml
@model WebApplication3.ViewModels.mainViewModel
<tr>
    <td>Category&nbsp;</td>
    <td>&nbsp;:</td>
    <td><div id="KnjigeNormativa">@Html.Partial("partKnjigeNormativaView", Model.ngrupa)</div> </td>
</tr>
<tr>
    <td>Sub - Category&nbsp;</td>
    <td>&nbsp;:</td>
    <td><div id="GrupeRadova"> @Html.Partial("partGrupeRadovaView", Model.gradova)</div></td>
</tr>
<tr>
    <td>Products&nbsp;</td>
    <td>&nbsp;:</td>
    <td><div id="Pozicije"> @Html.Partial("partPozicijeView", Model.pozicije)</div></td>
</tr>

your main controller 您的主控制器

public ActionResult Index(string SelectedPosId, int? page)
{
int pageNumber = (page ?? 1);
mainViewModel mv = new mainViewModel();
mv.ngrupa = //code for ngrupa list
mv.gradova = //code for ngrupa list
mv.pozicije = (from p in mainViewModel.getPozicije()
               where p.grupa == SelectedPosId
               order by p.id //order column here
               select p).ToPagedList(pageNumber, 25);
return View(mv);
}

your partial view 您的局部看法

//your paged partial view partPozicijeView.cshtml
@model PagedList.IPagedList<WebApplication3.ViewModels.Pozicije>
@using PagedList.Mvc;

@{ some code to show table }
@Html.PagedListPager((IPagedList)ViewBag.ResultsPage,


page => Url.Action("Index", "ControllerName", new { SelectedPosId = YourPosId, page = page},PagedListRenderOptions.PageNumbersOnly)

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

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