开发者

Create Ajax voting system

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

I want to implement a comments syste开发者_JAVA技巧m 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


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.


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");}
                    });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜