0

I have a search page which uses a simple ajax request to get new search results from the controller.

The controller returns the results as rendered html, so all the client script has to do is $('#results').html(data);

The html also contains paginated links.

I want to add click handlers to these paginated links inside the ajax success event handler, but I find it kind of icky to be getting the page number from the A's text property.. hypothetically, the links could read "Page 1", "Page 2", and then i'd be parsing text.

Am I doing this wrong?

Should I be generating the ajax links inside the partial view instead?

4
  • what mvc view engine do you use? Commented Jun 20, 2012 at 1:47
  • 1
    List your code, and ask a specific question please. Commented Jun 20, 2012 at 1:54
  • My question is specific, you have just failed to understand it. Your generic "here are some links" response which you deleted is proof of that. Did you downvote my question because I rejected your answer? Commented Jun 20, 2012 at 2:31
  • I have asked for feedback to better understand your code. Your response showed that you have no respect to people who would like to help "This is a generic response which doesn't help me in any way"- that was your response, and you down-vote me for trying to help you... You are a strange person. Commented Jun 20, 2012 at 2:34

1 Answer 1

1

The page number can be stored as data for a element. You can refer to that data in hooking up your event handlers. Traditionally--i.e. in my old applications--I would do it like this:

<a href="#" name="myLink" data="<%: [pageNumber] %>">
  Page <%: [pageNumber] %>
</a>

Then on the jquery side (i.e., ajax callback):

$('a[name=myLink]').click( function (e) {
  e.preventDefault();
  var pageNumber = $(this).attr('data');
  // do what you have to do with the pageNumber
});

But you can (and should) refer to the jQuery data function if you are going down this route.

Sign up to request clarification or add additional context in comments.

6 Comments

This is one of the approaches I considered. What makes it difficult is that PagedList.MVC does not support custom attributes on the links.
You say "in your old applications". Have you got any alternatives?
If I were to rewrite my old applications then I would use the $.data() function mentioned above, assuming of course I have full control of the rendered html. I wasn't aware you're using PagedList.MVC library, and honestly I'm not familiar with it. But based on its documentation--github.com/TroyGoode/PagedList, you may be able to make some manual adjustments.
Question: do you think it is more correct to add handlers this way, or to generate the partial view with links already included - ie.. <a href="changePage(1)">1</a>
That could work, if you can make this work--> <a href="changePage(1)">1</a> (Refer to stackoverflow.com/questions/134845/…). What are you going to do with the page number anyway? Can a controller call be more appropriate instead? <a href="controllerAction?Page=1">1</a>
|

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.