raklos
raklos

Reputation: 28545

Create Ajax voting system

I'm using asp.net mvc 3 and jquery.

I want to implement a comments system on my site with the ability to up vote and down vote comments. I want the voting to be done via Ajax (without redirect). It should update the votecount and prevent further voting on that comment.

Can someone explain how I can do this. What jquery functions should I call and how to use them?

Thanks

Upvotes: 1

Views: 992

Answers (2)

Nirmal
Nirmal

Reputation: 1245

You can design a generic handler (.ashx) to deal with the database and call it by using the ajax call. For example:

[WebService(Namespace = "http://www.mysite.com/webservices")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class UpVote : IHttpHandler
{  public void ProcessRequest(HttpContext context)
    { 
       // Create this method to deal with your database
       MakeUpVote(context.Request["commentID"].tostring());   // Comment ID is the input
} }

Now Call the Method by Ajax call

$.ajax({
            url: "UpVote.ashx",
            type: "POST",
            data: ({ "commentID": commentID }),
            success: function(result) {
 alert ("You have upvoted");}
                    });

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038810

Your question is very broad. You might use the $.ajax() function to send an AJAX request to a controller action which would update the vote count into the database given a question id:

$.ajax({
    url: '<%= Url.Action("VoteUp") %>',
    data: { questionId: 1234 },
    success: function(result) {
        alert('thanks for upvoting this question');
    }
});

Obviously the controller action should check if the currently logged in user hasn't already upvoted this question.

Upvotes: 1

Related Questions