Stephen
Stephen

Reputation: 803

.net mvc limit results with show more link

I would like to only display 2 results on my partial view with a show more link displayed if there are more results. My query which will display all results is:

[ChildActionOnly]
public ActionResult GetContacts(int id)
{
    var contacts = from c in db.CustomerContact
                   where c.CustomerID == id
                   orderby c.Surname
                   select c;

    return PartialView("_GetContacts", contacts);
}

Do I need to have two queries, one to count the number of results?

Would like to use jquery to show more results by expanding div.

If anyone could point me in the right direction it would be appreciated.

Upvotes: 0

Views: 1280

Answers (2)

Mahmoud Gamal
Mahmoud Gamal

Reputation: 79959

You can do this:

var contacts = ....
return contacts.OrderBy(c => c.curname)
    .Skip((CurrentPage - 1) * pageSize)
    .Take(pageSize);

You have to parameters here pagesize and currentPage for the first two items, pass pageSize = 2 and currentPage = 1 for more items(2 items per time) pass current page = 2 and so on.

Upvotes: 0

Bardo
Bardo

Reputation: 2523

Depending on the quantity of elements that your query usually returns it would be a good idea to use a query to retrieve just two elements and use a different query to show the rest of elements when your users ask for it.

To avoid loading all your results always you can use an AJAX call to server on the link to show more.

However, if your query usually returns few elements you can use just one query to load all of them and show two or more through show more link click.

Upvotes: 1

Related Questions