加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 如何在Asp.Net MVC中做部分帖子?

发布时间:2020-12-16 07:27:25 所属栏目:asp.Net 来源:网络整理
导读:我是asp.net mvc的新手. 我想创建一个网站,允许访问者进行部分发布,例如允许访问者按下类似按钮投票. 如何在asp.net mvc中执行此操作? 解决方法 您可以使用 Ajax实现此功能,浏览器将发送“幕后”帖子,以便说明,而无需重定向用户.服务器将在 JSON format中返
我是asp.net mvc的新手.

我想创建一个网站,允许访问者进行部分发布,例如允许访问者按下类似按钮投票.

如何在asp.net mvc中执行此操作?

解决方法

您可以使用 Ajax实现此功能,浏览器将发送“幕后”帖子,以便说明,而无需重定向用户.服务器将在 JSON format中返回数据.

在服务器上:创建一个新的Controller CommentsController并添加一个Action Like:

[Authorize] /*optional*/
public JsonResult Like(int id)
{
    //validate that the id paramater
    //Insert/Update the database
    return Json(new {result = true});
}

在您看来,只需使用jQuery Ajax方法:

function likeComment(id) {
    $.post('<%=Url.Action("Like","Comments")%>/' + id,function(data){
        //Execute on response from server
        if(data.result) {
            alert('Comment liked');
        } else {
            alert('Comment not liked');
        }
    });
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读