我是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/