views:

31

answers:

1

I'm building an ASP.NET MVC site where I want one of the Views I return to be automatically scroll to a certain point.

The part of the site where I want this to occur works sort of like a forum - there are "threads" that contain "posts". A user can either browse to the whole paginated thread or can browse to a specific post, using its ID. When a user browses to a specific post, I want to show the regular thread interface, then browse to the page that the post is on and scroll down to the post.

Is it possible to somehow automatically scroll down to a certain point when returning a View from an action in an ASP.NET MVC site? If so, how do I do this?

NOTE: One solution to this problem that I've found is how Stack Overflow and the other Stack Exchange sites do it: each answer to a question can be linked to by adding #ID to the URL. If it's impossible to automatically scroll down when returning a View, I would implement this instead, but I don't understand how to use such an approach when there are multiple pages and the post in question isn't on the current page.


UPDATE:

Based on Chris' answer, I'm currently planning to implement it with the URL looking like this: example.com/forum/[ForumID]/thread/[ThreadID]/post/[PostID]#[PostID]. In my Action, I figure out what page of the Thread the Post is on, and then I return all the Posts from that page to the View.

However, I noticed something special in how Stack Overflow solves this problem. Try going to: http://meta.stackoverflow.com/questions/57170 - it ends up sending you to http://meta.stackoverflow.com/questions/57155/gravatar-bugs-and-improvements-in-chat/57170#57170.

How is the above implemented? This is exactly what I want to accomplish.

+2  A: 

You can use javascript in your View to achieve automatic scrolling, with something like the jQuery ScrollTo plugin (http://plugins.jquery.com/project/ScrollTo). Attach it to your page load event and target your desired post.

However, the simplest way (and most cross-browser compatible) would be like how StackOverflow does it, with the #ID in the URL.

Chris
@Chris: about the `#ID` way: how do I make it work across pages? After all, I should only load one page at a time, right?
Maxim Zaslavsky
Make the link to the post the full link. So instead of your href being (for example) just #ID, use http://example.com/forum/2/thread/100/Page/2/#ID
Chris
Unless I find another solution, I will implement it to be like `example.com/forum/[ForumID]/thread/[ThreadID]/post/[PostID]#[PostID]`. This is because hardcoding the page number in the link isn't really good, as the page number can change (in my system). I think I will first read the PostID, return the page that that Post is on, , and then the `#` will activate.
Maxim Zaslavsky
Quick question: I understand how this works in most cases, but I noticed that on Stack Overflow there's something special, which is the exact thing that I'm looking for. **Try going to: http://meta.stackoverflow.com/questions/57170/** - it ends up sending you to http://meta.stackoverflow.com/questions/57155/gravatar-bugs-and-improvements-in-chat/57170#57170. **How is this implemented?** This is what I want to accomplish.
Maxim Zaslavsky
It's done by looking up the answer ID and redirecting to the correct question page with a #ID to jump to the correct answer. Essentially it does what it sounds like you were thinking of implementing.
Chris
@Chris - how do they add a `#` to the URL when redirecting? Could you give me some sample code for that?
Maxim Zaslavsky