0

I am working on asp.net mvc3 application and have many records coming from database. I want to display only 10 records first then user can click on button to see next 10 records and so on... Like facebook wall posting more records.

How can I implement this thing in my application using jQuery and ajax?

How can I control display data in view using paging?

I am using this to get 10 records but I want to display all records using more record button

var Entity = (from a
    in context.Data
    where <Condition>
    select a).Take(10);
Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
Bhargav
  • 451
  • 3
  • 13
  • 26

4 Answers4

1

The following articles should give you an idea :

http://weblogs.asp.net/andrewrea/archive/2008/07/01/asp-net-mvc-quot-pager-quot-html-helper.aspx

http://weblogs.asp.net/gunnarpeipman/archive/2010/02/21/simple-pager-for-asp-net-mvc.aspx

On the other hand, you can implement it like this :

Get the nuget package called TugberkUg.MVC

Then, your controller should look like below :

    public ActionResult Index(int page = 0) {

        const int pageSize = 10;

        #region _filter the model

        IQueryable<myModel> model = _myrepo.GetAll()

        #endregion

        #region _convert the model to paginatedList

        var paginatedModel = 
            new TugberkUg.MVC.Helpers.PaginatedList<myModel>(model, page, pageSize);

        #endregion

        return View(paginatedModel);
    }

And, here how your controller should look like :

@model TugberkUg.MVC.Helpers.PaginatedList<myModel>

@foreach(var item in Model) { 

     <p>@item.id</p>

}

You need to handle the pager as well and here is a sample for you :

ASP.NET MVC PaginatedList Pager - Put wise "..." after certain point

This TugberkUg.MVC.Helpers.PaginatedList class will provide all the necessary fields for pager.

Community
  • 1
  • 1
tugberk
  • 57,477
  • 67
  • 243
  • 335
0

I'm not aware of any .net library that will do pagination for you out of the box, so I will roll out a DIY solution for you to think about.

How can i implement this thing in my application using jquery and ajax?

It is probably wise to have a specific Controller for ajax requests. Just have a separate Area (Ajax), and send your ajax requests to that url you set up.

How can i control display data in view using paging?

Set up a controller that takes in a "page" parameter.

public ActionResult GetData(int? page){ // page is nullable to allow for default page

// Do your query here to get the specific page.

}

I'm not sure if you require more information other than this. If I were trying to do what you were doing, this is what I would do. Hope that helps.

Daryl Teo
  • 5,394
  • 1
  • 31
  • 37
0

If your are using Webgrid to display data then rowsperpage will do.

 var grid = new WebGrid(source: Model, selectionFieldName: "SelectedRow", rowsPerPage: 10, canPage: true, canSort: true);
Hari Gillala
  • 11,736
  • 18
  • 70
  • 117
0

I would suggest using a telerik mvc control. They are really easy to use, powerful and free.

www.telerik.com

John S
  • 464
  • 1
  • 8
  • 23