Go: MVC Paging

Source: Internet
Author: User

Original address: http://www.cnblogs.com/iamlilinfeng/p/4075292.html

Paging always make me very annoying, also because just contact, seems to have a lot of plug-ins, before with a, but later found a page of the original filter is not submitted, and then changed this, in fact, this article for me the most effective or view file in the BeginForm parameters

New RouteValueDictionary {"id", ""}}, perhaps the original paging method with this parameter is also possible, have not tried.
Objective

A few days ago in the blog hung a group of links, is the above link, from the blog a lot of friends, from the previous tutorial, the problem of MVC is concentrated in the following two parts "Contrl and view data transfer (multi-table data)" and data paging . Therefore, stay up late this evening to write a small demo to help more beginners quick start, due to time, there is no understanding of the place please dabigatran consultation, no tube will not, have asked a must answer. think it's helpful, don't forget to order a nice-huh.

Directory

First, Contrl and view data transfer (multi-table data)

Second, pagination control introduction

Third, MVC source code description

Iv. Source code Download

V. MVC Introductory Tutorial Directory

First, Contrl and view data transfer (multi-table data)

The garden is also written in MVC, and we use the home page of the garden for an example.

I only cut the garden part of the home page, divided into 4 pieces of content. And these 4 pieces of content may come from different data tables, assuming: the first to second piece of content from the blog table, the third to fourth block of content from the ad table. At this point, MVC generally has two ways of interacting with control and view.

1) viewbag variable mode

Using 4 viewbag Variables for data transfer, DATA1, Data2, Data3, and DATA4 data are transferred directly from the database.

The pseudo-code in control is as follows:

1 public         ActionResult Cnblogindex () 2         {3             viewbag.data1 = data1;4             viewbag.data2 = data2;5             VIEWBAG.DATA3 = data3;6             viewbag.data4 = data4;7             return View (); 8         }

The pseudo code in view is as follows:

1//First block content 2 @foreach (Data1 data in (viewbag.data1 as ienumerable<data1>)) 3 {4     <tr> 5         <td> @Htm L.displayfor (model = data.id) </td> 6     </tr> 7} 8//second block Content 9 @foreach (Data2 dat1 in (Viewbag.data2 as I enumerable<data2>)     <tr>12         <td> @Html. displayfor (model = data.id) </td>13     </tr>14}15//Third block content @foreach (Data3 data in (viewbag.data3 as Ienumerable<data3>)) (     <tr&) gt;19         <td> @Html. displayfor (model = data.id) </td>20     </tr>21}22//Fourth block content @foreach ( DATA4 data in (viewbag.data4 as ienumerable<data4>)) {     <tr>26         <td> @Html. Displayfor ( Model = data.id) </td>27     </tr>28}

2) ViewModel Way (recommended)

What is ViewModel? ViewModel is the model for the view, which makes the model more suitable for view. Data1, Data2, DATA3, DATA4 data are taken directly from the database and assembled to Viewmodel,viewmodel as the data carrier for the entire page:

The pseudo-code in ViewModel is as follows:

1 public     class Cnblogindex {2         list<data> Data1 {get; set;} 3         list<data> Data2 {get; set;} 4         list<data> Data3 {get; set;} 5         list<data> Data4 {get; set;} 6     }

The pseudo-code in control is as follows:

         Public ActionResult Cnblogindex ()         {             Viewmodel.cnblogindex cnblogindex = new Viewmodel.cnblogindex ();             Cnblogindex.data1 = Data1;             Cnblogindex.data2 = Data2;             Cnblogindex.data3 = Data3;             CNBLOGINDEX.DATA4 = Data4;             Return View (Cnblogindex);         }

The pseudo code in view is as follows:

1 @model cnblogindex 2 @foreach (var info in model.data1) 3     {4         <tr> 5             <td>info.**</td> 6
   </tr> 7     } 8 @foreach (var info in model.data2) 9     {         <tr>11             <td>info.**</td>         </tr>13     }14 @foreach (var info in model.data3)     {         <tr>17             <td>info.* *</td>18         </tr>19     }20 @foreach (var info in model.data4)     {         <tr>23             <td>info.**</td>24         </tr>25     }

Two ways to pass the data can do our work, but it is recommended to use ViewModel to aggregate the information of a page, so that although there is a bit more work, it can make the overall structure clearer, and the integration is easier to maintain.

Second, pagination control introduction

Many friends in the first contact with MVC, like to write their own page, here is recommended to use the open source paging control "Mvcpager", I believe that when you use WebForm, most of the paging controls used are Aspnetpager (Webdiyer produced), This MVC paging control is also webdiyer produced (very good very strong), the official blog: http://www.webdiyer.com/, I also mentioned in the previous tutorial the pagination control, but the group of friends reaction to my previous tutorial pagination control with the query condition, the page after the condition disappears. So, in some of the pagination controls I'll explain again, and add the query criteria.

The effect is as follows:

The query condition uses a text box and is transferred in view and control using the parameter. Here is just a way to use Mvcpager (get query mode), Mvcpager has a variety of paging methods, such as Ajax paging. Interested can download the code under research, the official address is as follows: http://www.webdiyer.com/mvcpager/

The code in ViewModel is as follows:

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using WEBDIYER.WEBCONTROLS.MVC; 6  7 Namespace MvcApplication.Models.Home 8 {9 Public     class Index10     {One public         string Blogurl {get; set; }12         //Current user public         model.user users {get; set;} +         //Information list public         pagedlist<model.info> infos {get; set;}     }17}

The code in control is as follows:

 1 public actionresult Index (int = id = 1, string title = null) 2 {3//user 4 Model. User user = new Model.user (); 5 User.userid = 1; 6 user.username = "MVC Technology Exchange (Group number: 384185065)"; 7//Information list (data is presented here using pagination controls) 8 int totalcount = 0; 9 int pageIndex = ID?? 1;10 int pageSize = 2;11 pagedlist<model.info> Infopager = DAL. Info.getinfos (title, PageSize, (pageIndex-1) * 2, out totalcount). AsQueryable (). Topagedlist (PageIndex, pageSize); infopager.totalitemcount = totalcount;13 infopager.currentpage             index = (int) (ID?? 1); 14//Data assembly to viewModel15 Models.Home.Index Index = new Models.Home.Index (); 16 Index. Blogurl = "http://www.cnblogs.com/iamlilinfeng/archive/2013/04/01/2992432.html"; User = user;18 index. Infos = infopager;19//------------------pass data using the VIEWBIG variable---------------//20//viewbag.pagerdata = infopager;21 return View (index); 22} 

The code in view is as follows:

 1 @model MvcApplication.Models.Home.Index 2 @using WEBDIYER.WEBCONTROLS.MVC; 3 <title>mvc Getting Started Tutorial </title> 4 5 @*----------------------------------blog address in ViewModel, MVC Getting Started Tutorial Blog address:/http www.cnblogs.com/iamlilinfeng/archive/2013/04/01/2992432.html-----------------------*@ 6 @Html. Label ("blog Address:") @ Model.blogurl 7 <br/> 8 @Html. Raw ("------------------------------------------------------------------------- -------------------------------------------------------------------------------------------") 9 <br/>10 11 @ *----------------------------------ViewModel User entity information-----------------------*@12 @Html. labelfor (model = model. User.userid) @Html. Raw (":") @Model. User.userid13 <br/>14 @Html. labelfor (model = model. User.username) @Html. Raw (":") @Model. User.username15 <br/>16 @*---------------------------------- ViewModel Data Information Paging-----------------------*@18 @Html. Raw ("--------------------------------------------------------- -----------------------------------------------------------------------------------------------------------") <br/>20 @using (Html.BeginForm (" Index " , "Home", new RouteValueDictionary {{"id", ""}}, Formmethod.get)) {@Html. Label ("Query criteria (title):") <input name= "T Itle "Value=" @Request. querystring["title"] "/><input type=" submit "value=" Query "/>23}24 <table>25 <tr >26 <th> numbering </th>27 <th> title </th>28 <th> content </th>29 &LT;/TR&G T;30 @foreach (var info in Model.infos) {<tr>33 <td> @Html. Displayfor (Model =& Gt info.infoid) </td>34 <td> @Html. displayfor (model = info. Title) </td>35 <td> @Html. displayfor (model = info. Content) </td>36 </tr>37}38 @*----------------------------------pass data using Viewbig variables--------------- --------*@39 @* @foreach (Model.info Info in (Viewbag.pagerdata as ienumerable<model.info>)) 40 {41 <tr>42 <td> @Html. displayfor (model = info. infoid) </td>43 <td> @Html. displayfor (model = info. Title) </td>44 <td> @Html. displayfor (model = info.          Content) </td>45 </tr>46}*@47 </table>48 @Html. Pager (Model.infos, New PAGEROPTIONS49  {pageindexparametername = "id", Wuyi showpageindexbox = true,52 Firstpagetext             = "Home", Prevpagetext = "Previous Page", NextPageText = "Next Page", Lastpagetext = "Last", 56             Pageindexboxtype = pageindexboxtype.textbox,57 pageindexboxwrapperformatstring = "Please enter page number {0}", 58 Gobuttontext = "goto") @Html. Raw ("total:") @Model. Infos.totalitemcount @Html. Raw ("page,") @Model. infos.current PageIndex @Html. Raw ("/") @Model. Infos.totalpagecount @Html. Raw ("page")
Third, MVC source code description

Development environment: VS2013,MVC4. The solution consists of 3 projects. Mvcapplication is the MVC presentation layer, model for data entities and database one by one corresponding to the DAL linked data for data access.

1,mvcapplication--models--index, for the ViewModel of index in view, to assemble all the data needed for index

2,model--info, which is the information entity, corresponds to the information table. Used to explain paging data

3,dal--sqlcommon, a class that invokes a paged stored procedure.

The data presented in view is provided by: User, list<info>, and with this example, more data can be assembled in this way.

Iv. Source code Download

Click I download the source code

V. MVC Introductory Tutorial Directory

0. No nonsense MVC Starter Tutorial Catalog page

1. No nonsense MVC Introductory Tutorial One [overview, Environment installation, create project]

2. Non-nonsense MVC Introductory Tutorial II [first small demo]

3. No nonsense MVC introductory Tutorial three [getting Started with routing settings and views]

4. Non-nonsense MVC introductory Tutorial four [layout using in the view]

5. No nonsense MVC introductory Tutorial Five [Control and view interaction]

6. No nonsense MVC introductory Tutorial VI [validation rules in model]

7. Non-nonsense MVC Introductory Tutorial VII [using AOP in control]

8. Non-nonsense MVC introductory Tutorial VIII [use of mvcpager pagination control]

9. No Nonsense MVC Introductory Tutorial IX [Combat one: User registration and landing]

10. No nonsense MVC Introductory Tutorial Ten [actual Combat II: User Management]

Go: MVC Paging

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.