2

I still haven`t found any example of sorting implementation through paging in ASP.NET MVC
(sort by name->press page 2->page 2 is still sorted by name).

I could "hack" it, make it dirty, but i`m sure there have to be good "how-to" guides for this.

What about sorting by two columns?

2 Answers 2

4

Just keep the sort expression in your model view and write it to the pagination route links.
For example like:

/MyEntity/Page/2?sort=Name

Or with custom routing like:

/MyEntity/Page/2/Name

For the latter the route mapping would look like:

{controller}/Page/{pageIndex}/{sortExpression}
Sign up to request clarification or add additional context in comments.

1 Comment

If i remember correct - thing that confused me was that my views "had to know about", as i thought - database structure. You know - sort expressions kind a maps with column names. Funny. It's always nice to find out how dumb i was.
2

I do it exactly the way aleris does except I use a enum field on my model for the sort values, this way it will fall back on the default if they enter a sort paremeter that doesn't exist.

public enum SortArticle
{
   Title,
   Published
}

public enum SortOrder
{
   Asc,
   Desc
}

articles/{sort}/{order}/{page}
articles/published/desc/1

1 Comment

These answers made me just a little bit less confused. Still cant visualize this A-Z. Ill try to create a small example project for myself. If that will be success, Aleris will get my "accepted answer". :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.