Andrés Orozco
Andrés Orozco

Reputation: 2671

How to update database without refreshing the page ASP.NET

We are creating a simple application using ASP.NET MVC. The app is all about uploading pictures and rating it, we have two buttons to vote, "like" and "dislike"

enter image description here

Each time I click the like button (green), the number besides it adds 1, and when I click the dislike button (red) the same thing happens, and the last number is the sum of these two, we are doing this using javascript. This is mostly like YouTube's likes/dislikes counter, and now, what we want to do, it to update this counters to the database each time a user votes, if possible we'd like to do it without refreshing the page.

Upvotes: 0

Views: 1347

Answers (1)

Mark
Mark

Reputation: 4883

Click a button, pass a variable and post to your method using ajax.

Something like this should get your started:

<button value="1">Like</button>
<button value="-1">Dislike</button>
<Script>
    $("button").click(function(){
        var xButtonValue = $(this).val();
        $.post("YourController.whatever",
        {
            value: xButtonValue,
        },
        function(data, status){
            alert("Data: " + data + "\nStatus: " + status);
        });
    });
</Script>

Upvotes: 2

Related Questions