【发布时间】:2019-06-18 09:35:20
【问题描述】:
我正在为每个评论构建一个“喜欢”按钮,并使用 jQuery 将数据发布到PostsController。如何为循环中的每个项目传递 ID 值@item.Id?在 jQuery 代码中处理它的正确方法是什么?
@foreach (var item in Model.PostComments)
{
<a id="@item.Id" class="btn btn-success"><span class="glyphicon glyphicon-thumbs-up"></span></a>
}
$(document).ready(function() {
$("#@item.Id").click(function() {
var FollowOptions = {};
FollowOptions.url = "/Posts/CommentUp/";
FollowOptions.data = { id: "@Model.PostComment.Id" };
$.ajax(FollowOptions);
});
});
public IActionResult CommentUp(Guid id)
{
PostComment PostComment = _context.PostComment.Where(m => m.Id == id).SingleOrDefault();
if (PostComment == null)
{
return NotFound();
}
string currentuserid = _userManager.GetUserId(User);
if (_context.CommentMetric.Where(f => f.PostCommentId == id && f.ApplicationUserId == currentuserid).Count() == 0)
{
_context.CommentMetric.Add(new CommentMetric
{
Id = Guid.NewGuid(),
ApplicationUserId = currentuserid,
PostCommentId = id,
VoteValue = 1
});
return RedirectToAction("Details/" + id);
}
【问题讨论】:
-
您是否只想将一个 id 传递给控制器?
标签: jquery asp.net-mvc razor asp.net-core .net-core