gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 22:51:19 27 4
gpt4 key购买 nike

我是asp.net mvc 的新手。

我想创建一个网站,允许访问者发布部分帖子,例如允许访问者按 like按钮对评论进行投票。

如何在 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');
}
});
}

关于asp.net-mvc - 如何在 Asp.Net MVC 中发布部分帖子?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4999830/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com