I am building a 'like' button for each comment and posting data using jQuery to PostsController
. How can I pass the Id value, @item.Id
, for each item in the loop? Whats the proper way to handle it in jQuery code?
@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);
}