tags:

views:

66

answers:

2

I am writing a learning project in ASP.NET MVC. I have a page "Books" that display a user's books and there are a few sorting and filtering options in that page like this.


Book Page

Sorting Parameters : Title, Year,Author... Filtering Parameters : Read, Unread ...


When user selects a sorting parameter like "Title", Controller sorts books by title and returns to view.Then if user selects "Read" filter I want listed books to be sorted by "Title" and filtered by "Read" parameter.

As far as I know, I must store sort and filter parameters to do this.I am not sure what is the best and easy way because of I am a bit confused about Session,HttpContext,Tempdata concepts in ASP.NET MVC. I can do this using classical ASP.NET Webforms Session way but some people says using session may introduce problems . What is the alternatives, what is best and easy way to do this?

A: 

It depends on whether you want the selections to be persistent as the user navigates around the site and then returns to the page. If it needs to be persistent, then storing the choices in the session is a reasonable way to do this. If the selections are only for that particular page, then you can put the selections into the ViewData and have the selections in the view driven by the values in the ViewData (or Model). This way the choices for every round trip result in the correct defaults when the page is returned to the browser.

My personal feeling is that the choices ought to be retained for the whole session and I keep my values stored in the session.

tvanfosson
A: 

There are some great options for you with the MVC Framework. One is having a hidden input on your form that stores the information for you. So when you hit a resort button then it keys in on that field to determine the sort order.

A second more elegant way of doing it is having a value associated with your button, so if I hit title it sorts by title, if i hit author it sorts by author. The controller will be able to detect this.

Lastly there are some excellent javascript controls you can use that automatically do all this for you. One that comes to mind is EXT Grid control which is absolutely awsome. It involves some work with javascript but its totally worth it! Hope this helps

Al Katawazi