I am new at Stackoverflow and this is my first question.
I am making my first blog site, and want a like button, like facebook.
2 problems with my like button.
It wont update like counts in articles with same data-id. I was only avaible to update (this) article-link
When an user likes/dislikes an article, should be able to unlike without refreshing page. It is because it wont check if there is data in database without refreshing page. How can i do that without refresh?
Controller:
public string LikeThis(int id)
{
Article art = DB.Articles.FirstOrDefault(x => x.ID == id);
if (User.Identity.IsAuthenticated || Session["Username"] != null)
{
var Username = User.Identity.Name;
Member m = DB.Members.FirstOrDefault(x => x.Username == Username);
art.Likes++;
Like like = new Like();
like.ArticleID = id;
like.UserID = m.ID;
like.LikedDate = DateTime.Now;
like.Liked = true;
DB.Likes.Add(like);
DB.SaveChanges();
}
return art.Likes.ToString();
}
public string UnlikeThis(int id)
{
Article art = DB.Articles.FirstOrDefault(x => x.ID == id);
if (User.Identity.IsAuthenticated || Session["Username"] != null)
{
var username = User.Identity.Name;
Member m = DB.Members.FirstOrDefault(x => x.Username == username);
Like l = DB.Likes.FirstOrDefault(x => x.ArticleID == id && x.UserID == m.ID);
art.Likes--;
DB.Likes.Remove(l);
DB.SaveChanges();
}
return art.Likes.ToString();
}
View:
<li>@{
int userid = Convert.ToInt32(Session["ID"]);
if (!User.Identity.IsAuthenticated)
{
<i class="fa fa-heart-o fa-lg"></i><span>(@art.Likes)</span>
}
else if (art.Likes1.Any(x => x.UserID == userid && x.ArticleID == art.ID) || Ajax.ViewBag.Liked == true)
{
<a href="javascript:void(0)" class="unlike" data-id="@art.ID"><i class="fa fa-heart fa-lg text-danger"></i><span>(@art.Likes)u</span></a>
}
else
{
<a href="javascript:void(0)" class="like" data-id="@art.ID"><i class="fa fa-heart-o fa-lg"></i><span>(@art.Likes)l</span></a>
}
}
</li>
Ajax:
$(document).ready(function () {
//LIKE
$("a.like").click(function () {
var id = $(this).data("id");
var link = "/Article/LikeThis/" + id;
var a = $(this);
$.ajax({
type: "GET",
url: link,
success: function (result) {
a.html('<i class="fa fa-heart fa-lg text-danger"></i> (' + result + ')').removeClass("like").addClass("unlike");
}
});
});
//UNLIKE
$("a.unlike").click(function () {
var id = $(this).data("id");
var link = "/Article/UnlikeThis/" + id;
var a = $(this);
$.ajax({
type: "GET",
url: link,
success: function (result) {
a.html('<i class="fa fa-heart fa-lg text-danger"></i> (' + result + ')');
}
});
});
});
I don't hope that i missed anything. Hope u guys can help.