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?

NoPyGod
  • 4,905
  • 3
  • 44
  • 72
  • what mvc view engine do you use? – Yusubov Jun 20 '12 at 01:47
  • 1
    List your code, and ask a specific question please. – Yusubov Jun 20 '12 at 01: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? – NoPyGod Jun 20 '12 at 02: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. – Yusubov Jun 20 '12 at 02:34

1 Answers1

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.

Alex R.
  • 4,664
  • 4
  • 30
  • 40
  • This is one of the approaches I considered. What makes it difficult is that PagedList.MVC does not support custom attributes on the links. – NoPyGod Jun 20 '12 at 01:59
  • You say "in your old applications". Have you got any alternatives? – NoPyGod Jun 20 '12 at 02:00
  • 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--https://github.com/TroyGoode/PagedList, you may be able to make some manual adjustments. – Alex R. Jun 20 '12 at 02:10
  • Question: do you think it is more correct to add handlers this way, or to generate the partial view with links already included - ie.. 1 – NoPyGod Jun 20 '12 at 02:33
  • That could work, if you can make this work--> `1` (Refer to http://stackoverflow.com/questions/134845/href-tag-for-javascript-links-or-javascriptvoid0). What are you going to do with the page number anyway? Can a controller call be more appropriate instead? `1` – Alex R. Jun 20 '12 at 03:06
  • @NoPyGod if you have no problem to use another pagination library you can try this(http://mvcpager.codeplex.com/) it supports unobtrusive ajax as well. – VJAI Jun 20 '12 at 03:44