我在 C# asp.net MVC 项目中的投票系统出现问题.Idea基于StackOverflow投票系统



我正在尝试像StackOverflow一样在答案帖子上实现投票系统。我能够执行ajax请求,并且所有内容都以所需的方式完美地存储在数据库中。但是我的View面临问题.在我看来,所有answersHtml都是用一个for循环渲染的。在这个Html元素中,我通过div名为vote的类answerjQuery一起选择,并执行我的 ajax 调用并更新我的投票计数。但问题是,当我对一个答案投赞成票时,它不是只增加它的票数,而是同时增加所有答案的投票数。 我认为选择所有具有voteclass名称的div是错误的。但是我找不到一种方法来单独选择它们以增加我对所选答案的VoteCount。 这是我的View代码。

@model AskMe.Models.ViewModel.QuestionDetailViewModel
@{
ViewBag.Title = "QuestionDetails";
}
<h1>Question Details: </h1>
<h3>@Model.question.Post.Title</h3>
@Html.Raw(Model.question.Post.Content)
<hr />
<h3>Answers:</h3>
<div id="paginationContainer">
@foreach (var answer in Model.question.Answers)
{
<div class="card bg-light m-1">
<div class="card-body">
<h5 class="card-title">Answered by: @answer.Post.CreatedBy.UserName</h5>
@Html.Raw(answer.Post.Content)
</div>
<div class="col vote">
<div>
<button data-vote-id="@answer.Post.PostId" class="nav-link thumbs-up" href=""><i class="fa fa-thumbs-up" aria-hidden="true"></i></button>
</div>
<div class="Count">
@(answer.Post.UpVotes.Count-answer.Post.DownVotes.Count)
</div>
<div>
<button data-vote-id="@answer.Post.PostId" class="nav-link thumbs-down" href=""><i class="fa fa-thumbs-down" aria-hidden="true"></i></button>
</div>
</div>
@Html.ActionLink("View Comments", "Index", "Comments", new { answerId = answer.PostId }, null) |
</div>
}
</div>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h3>Contribute your answer: </h3>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(m => m.AnswerContent, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextAreaFor(m => m.AnswerContent, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
@Html.HiddenFor(m => m.UserId)
@Html.HiddenFor(m => m.QuestionId)
@Html.HiddenFor(m => m.question)
<input type="submit" value="Contribute now" class="btn btn-outline-primary" />
</div>
</div>
</div>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval");
<script type="text/javascript">
$(document).ready(function () {
tinyMCE.init({
mode: "textareas",
});
$('#paginationContainer').buzinaPagination({
itemsOnPage: 3,
prevnext: true,
prevText: "Previous",
nextText: "Next"
});
$(".vote").on("click", ".thumbs-up", function () {
$.ajax({
url: "/api/votes/upvote/" + $(this).attr("data-vote-id"),
method: "POST",
success: function () {
var countTxt = $(".Count").text();
console.log(countTxt);
var count = parseInt(countTxt);
$(".Count").text(count + 1);
console.log(count);
}
})
})
$(".vote").on("click", ".thumbs-down", function () {
$.ajax({
url: "/api/votes/downvote/" + $(this).attr("data-vote-id"),
method: "POST",
success: function () {
var countTxt = $(".Count").text();
console.log(countTxt);
var count = parseInt(countTxt);
$(".Count").text(count - 1);
console.log(count);
}
})
})
});
</script>
}

这是我的ViewModel

public class QuestionDetailViewModel
{
ApplicationDbContext _context = new ApplicationDbContext();
public virtual Question question { get; set; }
[Display(Name = "Add your answer.")]
public string AnswerContent { get; set; }
public string UserId { get; set; }
public int QuestionId { get; set; }
public int AnswerId { get; set; }
public int VoteCount { get; set; }
}

据我所知,您正在选择和设置所有.Count元素

var countTxt = $(".Count").text();
...
$(".Count").text(count - 1);

虽然我们应该只处理那些与当前答案相关的.Count元素。

可以使用祖先+查找选择器来完成

var $count = $(this).ancestor(".vote").children(".Count")[0];
$.ajax({  
url: "/api/votes/downvote/" + $(this).attr("data-vote-id"),
method: "POST",
success: function () {
var countTxt = $count.text();
...
$count.text(count - 1);

谢谢,@EugenePodskal指出我的错误并向我展示解决方案的正确方向。这是问题的解决方案。

$(".vote").on("click", ".thumbs-up", function () {
var counter = $(this).closest(".vote").children(".Count");
$.ajax({
url: "/api/votes/upvote/" + $(this).attr("data-vote-id"),
method: "POST",
success: function () {
var countTxt = counter.text();
console.log(countTxt);
var count = parseInt(countTxt);
counter.text(count + 1);
console.log(count);
}
})
})

再次感谢您@EUgenePodskal的帮助和时间。

最新更新