Yang Tao Teacher Mvcpager Example

Source: Internet
Author: User

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, newPagerOptions { 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(intpageIndex = 1,intid = 1){    if(Request.IsAjaxRequest())    {        stringtarget = Request.QueryString["target"];//通过target来区分获取的是哪个分页数据。        if(target == "blog1")        {            returnPartialView("_Blog1List"newPagedList<Blog1>(GetBlog1s(pageIndex), pageIndex, 10, 100));//页码参数名称为pageIndex        }        if(target == "blog2")        {            returnPartialView("_Blog2List"newPagedList<Blog1>(GetBlog1s(id), id, 10, 100));//页码参数名称为id        }    }    Blog blog = newBlog();    blog.Blog1s = newPagedList<Blog1>(GetBlog1s(pageIndex), pageIndex,10, 100);    blog.Blog2s = newPagedList<Blog2>(GetBlog2s(id), id, 10, 100);    return View(blog);}publicList<Blog1> GetBlog1s(intpageIndex){    List<Blog1> blog1s = newList<Blog1>();    for(int i = 0; i < 10;i++)    {        blog1s.Add(newBlog1(pageIndex, "name1-"+ pageIndex, "content1-"+ pageIndex));    }    returnblog1s;}publicList<Blog2> GetBlog2s(intid){    List<Blog2> blog2s = newList<Blog2>();    for(inti = 0; i < 10; i++)    {        blog2s.Add(newBlog2(id, "name2-"+ id, "content2-"+ id));    }    returnblog2s;}

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.