如何在不刷新ASP.NET页的情况下更新数据库 [英] How to update database without refreshing the page ASP.NET

查看:98
本文介绍了如何在不刷新ASP.NET页的情况下更新数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用ASP.NET MVC创建一个简单的应用程序.该应用程序仅需上传图片并对其评分,我们有两个按钮可以投票:喜欢"和不喜欢"

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"

每次单击喜欢"按钮(绿色)时,数字都会加1,当我单击不喜欢"按钮(红色)时,会发生相同的情况,最后一个数字是这两个之和,我们在做这使用JavaScript. 这主要类似于YouTube的喜欢/不喜欢"计数器,现在,我们要做的是,每次用户投票时,它将计数器更新到数据库中,如果可能的话,我们希望在不刷新页面的情况下进行操作.

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.

推荐答案

单击一个按钮,传递一个变量,然后使用ajax将其发布到您的方法中.

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>

这篇关于如何在不刷新ASP.NET页的情况下更新数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆