I'm trying to implement a voting system for a project I am working on.
I used this script in my Quiz.cshtml. The ajax part is an adaptition of a piece of code that I found on the SO side. I am not sure where the select, answer come from. Or, how it was implemented in the Controller?
I have these issues:
Save the action upon voting. Not sure how it should look like in the Controller. Or, basically get the AJAX code to work.
How can I link font awesome instead of image? The below code with font awesome works fine, seperately.
@*<div class="votecell post-layout--left"> <div class="votes"> <i class='fas fa-caret-up' style='font-size:48px;color:darkgrey'></i> <br/> <i class='fas fa-caret-down' style='font-size:48px;color:darkgrey'></i> </div> </div>*@
Notes: I use a MVC ASP.NET project coding in C#.
Cshtml file with AJAX code below:
<ul class="quizs">
@foreach (var quiz in Model)
{
<li>
<div class="details">
<div class="rating-star">
@for (int i = 0; i < 5; i++)
{
<i class="fas fa-star"></i>
}
</div>
</div>
<div id=@quiz.Id class="answer">
<img src="Vote_up.png">
<div class="score">0</div>
<img src="Vote_down.png">>
</div>
</li>
}
</ul>
@section scripts
{
<script>
$(function() {
$('div.answer img.vote').click(function() {
var id = $(this).parents('div.answer').attr('id').split('_')[1];
var vote_type = $(this).hasClass('up') ? 'up' : 'down';
if($(this).hasClass('selected')) {
$.post('/vote/', {id: id, type: vote_type}, function(json) {
if(json.success == 'success') {
$('#answer_' + id)
.find('img.' + vote_type);
.attr('src', 'vote_' + vote_type + '_selected.png')
.addClass('selected');
$('div.score', '#answer_' + id).html(json.score);
}
});
} else {
$.post('/remove_vote/', {id: id, type: vote_type}, function(json) {
if(json.success == 'success') {
$('#answer_' + id)
.find('img.' + vote_type);
.attr('src', 'vote_' + vote_type + '.png')
.removeClass('selected');
$('div.score', '#answer_' + id).html(json.score);
}
});
}
});
});
</script>
}
For the moment I get the following result, where it's not showing the up/down image