3

Well, how to create picture link like this up or down votes (on the left) from link below? (ajax enabled link)

<%= Ajax.ActionLink("Vote!",
                    "AddPictureVote",
                    "Vote",
                    new {id = Model.PictureId},
                    new AjaxOptions{UpdateTargetId = "addvote"})%>
1
  • In this case, I don't like the Ajax helper mechanism. I don't think that there is any reason really to get HTML back from the server, which is what the standard Ajax helper will do. I'd prefer to handle it with jQuery as I show in my example. Commented Jul 5, 2009 at 0:32

1 Answer 1

10

I think this is the basic idea. You can fill in the details/adapt as needed to your markup and model/actions.

$('.upvote').click( function() {
    $(this).addClass('highlight');
    $(this).nextAll('.downvote:first').removeClass('highlight');
    $.post( '<%= Url.Action( "vote", new { id = Model.ID } %>', { vote: 'up' } );
});

$('.downvote').click( function() {
   $(this).addClass('highlight');
   $(this).prevAll('.upvote:first').removeClass('highlight');
   $.post( '<%= Url.Action( "vote", new { id = Model.ID } %>', { vote: 'down' } );
});
Sign up to request clarification or add additional context in comments.

4 Comments

Few questions for you: 1) can you explain that last parametar {vote: "up"}? how can I cach that in controler? 2) is this an ajax post method?
Yes -- it's ajax. You'd have an action on the same controller called Vote that takes a string parameter vote: public ActionResult Vote( string vote ) { ... }
Note -- that it's only an example. You could send it a boolean as well -- with true being an upvote and false being a downvote.
Note that if you want the number of votes back so you can update it on the page, just send it back as a json response and use the success callback on the post method to update the vote count with the value returned from the server.

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.