Yang Tao Teacher plugin address: Http://www.webdiyer.com/mvcpager
Yang Tao The example on the website is very detailed, reference his examples and help documents can be used very well, experienced students can directly refer to the official example.
One, the standard Ajax paging
1. Create a new, empty MVC project
2. Search and install Mvcpager
3. How to add the controller
1//<summary
2///single Paging
3 //</summary>
4//<returns></returns> 5 [HttpGet] 6 public actionresult singlepage (int id = 1) 7 {8 list<article> articles = new list<article> (); 9 for (int i = 0; i < 10; i++) {Article A = new article (); a.id = id;13 A.title = "Title" + id;14 a.content = "Content" + id;15 articles. Add (a); 16}
Note: Crossing network questions There are some students after watching Yang teacher after the demo do not know how to go to the database according to the conditions to take the corresponding page data, rather than the collection of data out of the page, in fact, as long as the source download to see the implementation of the method inside the good. Pagedlist<article> model = new Pagedlist<article> (articles, ID, ten),//articles-current page record, id-page number , 10-records per page, 100-Total number of records if (Request.isajaxrequest ()) return Partialview ("_articlelist", model ); return View (model); 21}
4. Add view singlepage.cshtml, partial view _articlelist.cshtml, _articletable.cshtnlsinglepage.cshtml
1 @using WEBDIYER.WEBCONTROLS.MVC 2 @model pagedlist<mvcpagertest.models.article> 3 4 <div id= "articles" > 5 @Html. Partial ("_articlelist", Model) 6 </div> 7 @section scripts 8 {9 @{ Html.registermvcpagerscriptresource ();} 10}
_articlelist.cshtml
1 @using WEBDIYER.WEBCONTROLS.MVC 2 @model pagedlist<mvcpagertest.models.article> 3 4 <div class= "Text-center" > 5 @Ajax. Pager (Model, new pageroptions {pageindexparametername = "id", Containertagname = "ul", CssClass = "Pagin ation ", 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 ("articles")) 6 </div> 7 8 @{html.renderpartial ("_articletable");} 9 10 <div class= "Text-center" >11 @Ajax. Pager (Model, new Pageroptions {Alwaysshowfirstlastpagenumber = True, Pageinde Xparametername = "id", 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 ("articles")) </div>
_articletable.cshtnl
1 @using WEBDIYER.WEBCONTROLS.MVC 2 @model pagedlist<mvcpagertest.models.article> 3 4 <table class= "table Table-bordered table-striped "> 5 <tr> 6 <th class=" nowrap "> Serial number </th> 7 <th> 8 @Html. displaynamefor (model = model. Title) 9 </th>10 <th>11 @Html. displaynamefor (model = model. Content) </th>13 </tr>14 @{int i = 0;} @foreach (var item in Model) <tr>18 <td>@ (Model.startitemindex + i++) </ td>19 <td>20 @Html. displayfor (ModelItem = Item. Title) </td>22 <td>23 @Html. displayfor (ModelItem = Item. Content) </td>25 </tr>26 }27 </table>
5. The error occurs when running the program: The following sections are defined, but not yet rendered for the layout page "~/views/shared/_layout.cshtml": "Scripts". Workaround: Add code @RenderSection ("Scripts", Required:false) in _layout.cshtml to run the result:
Two, multiple Ajax paging
Multiple Ajax paging is similar to a single Ajax page, which should be noted here:
1, different paging control to define different page number parameter names (as follows: The first definition is pageindex, the second is defined as ID)
2, the background through the custom parameters to distinguish which paging data is obtained, here by Addroutevalue ("param", "value") to add
12 |
//PageIndexParameterName设置页码参数名称; @Ajax.Pager(Model).Options(o=>o.AddRouteValue("target","blog1"))生成分页链接的路由的值。
@Ajax.Pager(Model,
new
PagerOptions { AlwaysShowFirstLastPageNumber =
true
, PageIndexParameterName =
"id"
, 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>" }).Options(o => o.AddRouteValue(
"target"
,
"blog2"
)).AjaxOptions(a => a.SetUpdateTargetId(
"blog2s"
))
|
123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
/// <summary>
/// 多个分页
/// </summary>
/// <returns></returns>
[HttpGet]
public ActionResult MultiPage(
int
pageIndex = 1,
int
id = 1)
{
if
(Request.IsAjaxRequest())
{
string
target = Request.QueryString[
"target"
];
//通过target来区分获取的是哪个分页数据。
if
(target ==
"blog1"
)
{
return
PartialView(
"_Blog1List"
,
new
PagedList<Blog1>(GetBlog1s(pageIndex), pageIndex, 10, 100));
//页码参数名称为pageIndex
}
if
(target ==
"blog2"
)
{
return
PartialView(
"_Blog2List"
,
new
PagedList<Blog1>(GetBlog1s(id), id, 10, 100));
//页码参数名称为id
}
}
Blog blog =
new
Blog();
blog.Blog1s =
new
PagedList<Blog1>(GetBlog1s(pageIndex), pageIndex,10, 100);
blog.Blog2s =
new
PagedList<Blog2>(GetBlog2s(id), id, 10, 100);
return View(blog);
}
public
List<Blog1> GetBlog1s(
int
pageIndex)
{
List<Blog1> blog1s =
new
List<Blog1>();
for
(
int i = 0; i < 10;i++)
{
blog1s.Add(
new
Blog1(pageIndex,
"name1-"
+ pageIndex,
"content1-"
+ pageIndex));
}
return
blog1s;
}
public
List<Blog2> GetBlog2s(
int
id)
{
List<Blog2> blog2s =
new
List<Blog2>();
for
(
int
i = 0; i < 10; i++)
{
blog2s.Add(
new
Blog2(id,
"name2-"
+ id,
"content2-"
+ id));
}
return
blog2s;
}
|
Example Address (vs2015): Https://files.cnblogs.com/files/zhaorong0912/MvcPagerDemo.rar
Finally: The purpose of the article is to record their own learning, if there are errors, I hope you can point out in time, I greatly appreciate!
Yang Tao Teacher Mvcpager Example