I'm working on a site where users can add points to other user, however once a user added a point to other particular user he cannot add more points to the same user. So user1 can only give one point to user2.
On the server I check if there already exists a point with the same username. Everything works nicely so what I want to do now is to inform a user with some kind of message that he cannot add more points when he tries to do so since he already did add a point before. I think what i need is to return javascript from the server that will display the message. But maybe there is some other solution to this. Here is my action in controller:
[HttpPost]
public ActionResult AddPointAndCopyCurrentFavToPlaylist(int id)
{
if (CheckIfPointExists(User.Identity.Name, id))
{
var originalSong = repository.GetCurrentFav(id);
var newSong = new Song();
newSong.UserName = User.Identity.Name;
newSong.Title = originalSong.Title;
newSong.YoutubeLink = originalSong.YoutubeLink;
newSong.GenreId = 38;
newSong.Date = DateTime.Now;
repository.AddSong(newSong);
var point = new Point();
point.UsernameGotPoint = originalSong.UserName;
point.UsernameGavePoint = User.Identity.Name;
point.Date = DateTime.Now;
point.Score = 1;
point.CurrentFavId = id;
repository.AddPoint(point);
repository.Save();
return RedirectToAction("Index");
}
else return JavaScript(???);
}
here is my jquery Ajax:
$(".btnAddOtherSongPoints").click(function () {
var songId = $(this).attr("name");
$.ajax({
beforeSend: function () { ShowAjaxLoader(); },
url: "/Home/AddPointAndCopyOtherSongToPlaylist/",
type: "POST",
data: { id: songId },
success: function () { HideAjaxLoader(), ShowMsg("Song Added Successfully") },
error: function () { HideAjaxLoader(), ShowMsg("Song could not be added, please try again") }
});
});
based on whether he was not allowed to give as point in the success part of my ajax request I want to display a different message so i'm guessing I would need an if statement in there to check whether the data object is not null (that means the server returned something, in this case a javascript function or something like that)