一、项目中必须引用MVCpager.dll,现在是3.0版本。
二、添加页面view.Model
namespace Model.ViewModel { public class VolumeTree { public int VolumeID { get; set; } public string VolumeName{ get; set; } public List<Subsection> SubsectionS { get; set; } } public class Subsection { public int SubsectionID { get; set; } public string SubsectionName { get; set; } public string PoetryName { get; set; } } public class Poet { public int PoetID { get; set; } public string powtName { get; set; } } public class Poetry { public int PoetryID { get; set; } public string PoetryName { get; set; } public Poet poet { get; set; } } }
View Model
三、控制器代码,也是默认页面index
public ActionResult index(int? id = 1) { // List<Model.ViewModel.VolumeTree> VolunePage = new List<Model.ViewModel.VolumeTree>(); // int totalCount = 0; /*OperateContext.Current.BLLSession.IVolumeBLL.GetListBy_NoTrack(s => s.VolumeID > 0).ToList().Count();*/ int pageIndex = id ?? 1; // var VolunePage = OperateContext.Current.BLLSession.IVolumeBLL.getPageDate(pageIndex, 2,s=>s.VolumeID>0,s=>s.VolumeID, out totalCount,true); var VolunePage = OperateContext.Current.BLLSession.IVolumeBLL.GetListBy_NoTrack(s => s.VolumeID > 0).Select(s => new Model.ViewModel.VolumeTree { VolumeID = s.VolumeID, VolumeName = s.VolumeName, SubsectionS = s.Subsection.Select(u => new Model.ViewModel.Subsection { SubsectionID = u.SubsectionID, SubsectionName = u.SubsectionName, PoetryName = u.Poetries.Select(p => p.PoetryName).FirstOrDefault() }).ToList() }).ToList(); PagedList<Model.ViewModel.VolumeTree> mPage = new PagedList<Model.ViewModel.VolumeTree>(VolunePage, pageIndex, 5); //mPage.TotalItemCount = totalCount; //mPage.CurrentPageIndex = (int)(id ?? 1); if (Request.IsAjaxRequest()) { return PartialView("VolumeTreeMvcPager", mPage); } return View(mPage);
控制器代码
四、index页面cshtml,这是第一视图,获取model,同时传给VolumeMvcpagerTree.cshtml
@model PagedList<Model.ViewModel.VolumeTree> @using Webdiyer.WebControls.Mvc; @{ ViewBag.Title = "index"; Layout = "~/Views/Shared/_Layout.cshtml"; } <div> <h2>管理</h2> <div id="MVCpager"> @Html.Partial("VolumeMvcpagerTree", Model) </div> </div>
index页面
五、局部视图,将主页index的Model解析,绑定数据,用户呈现,主要是要把Mvcpager的分页html.help搞定
@model PagedList<Model.ViewModel.VolumeTree> @using Webdiyer.WebControls.Mvc; <ul> @if (Model != null) { foreach (var item in Model) { <li> @item.VolumeName<span>‘<a href="javascript:;">增加分卷</a></span> <ul> @if (item != null) { foreach (var Sub in item.SubsectionS) { <li>@Sub.SubsectionName</li> } } </ul> </li> } } </ul> @if (Model != null) { <div class="text-center"> @Ajax.Pager(Model, new PagerOptions { PageIndexParameterName = "id", FirstPageText = "首页", PrevPageText = "上一页", NextPageText = "下一页", LastPageText = "末页", NumericPagerItemCount = 5, ContainerTagName = "ul", CssClass = "pagination", CurrentPagerItemTemplate = "<li class=\"active\"><a href=\"#\">{0}</a></li>", DisabledPagerItemTemplate = "<li class=\"disabled\"><a>{0}</a></li>", PagerItemTemplate = "<li>{0}</li>" }).AjaxOptions(a => a.SetUpdateTargetId("MVCpager")) </div> }
时间: 2024-11-05 22:46:30