【问题标题】:Pagination for only one partial view in razor剃刀中仅一个局部视图的分页
【发布时间】:2014-03-26 07:13:33
【问题描述】:

我有 3 个类的视图模型

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

局部视图partPozicijeView.cshtml

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

@{ some code to show table }

和主视图 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>

调用 3 个局部视图以显示级联下拉列表中的前 2 个表和表中的第 3 个 (pozicije)。

现在我正在尝试向该表添加分页,但无论如何,我都会遇到错误

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]'.

第三次局部视图调用。

这里还有控制器的代码

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

我尝试(注释代码)返回 .ToPagedList() 但没有成功...

有人能指点我怎么做吗?

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-4 razor pagination


    【解决方案1】:

    试试这个:

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

    然后在视图中:

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

    相应地修改需要修改的内容。希望对您有所帮助。

    【讨论】:

    • 同样的错误...我认为这与我的部分视图 cshtml 中的模型声明与 index.cshtml @model PagedList.IPagedList中的声明不同>
    • 是的,这应该是问题所在。请同时显示 PartialView 代码
    • 我认为 PV 代码并不重要,只是 foreach 将数据放在 ”时抛出错误...
    • 换行到IEnumerable&lt;WebApplication3.ViewModels.mainViewModel&gt;有帮助吗?
    • 谢谢老兄..你拯救了我的一天,我在同样的问题上苦苦挣扎..你的回答很有帮助。
    • 标签中。当我启动应用程序时,它甚至没有被执行。第一次调用“
      @Html.Partial("partPozicijeView", Model)
    【解决方案2】:

    试试这个代码

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

    你的主控制器

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

    【讨论】:

    • 我没有将 ViewModels.Pozicije 声明为单独的类。这只是 mainViewModel 类中的一个属性。
    • 你已经在 mainViewModel 中声明了这个 pozicije 属性,但这是 Pozicije 的列表,即IEnumerable&lt;Pozicije&gt; 其中Pozicije 应该是一个声明的类
    • 它是一个独立于数据库的表模型。主视图模型使用这 3 个表格来显示 2 个下拉菜单和一个需要分页的表格。
    • ok 在你的局部视图中使用表名@model PagedList.IPagedList&lt;Pozicije&gt;
    • 这不是我一直在寻找的答案,但你帮了很多忙。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2012-03-28
    • 1970-01-01
    • 2018-04-24
    • 2011-10-10
    • 2012-04-02
    • 1970-01-01
    • 2013-09-28
    • 2012-04-13
    相关资源
    最近更新 更多