I'm trying to call a function using ajax, but it's not responding.
Here is the Code:
<p id="myElem" class="alert-danger" style="display:none"></p>
<button title="Remove" data-id="@item.Book.Book_id" class="Remove btn btn-group-sm red" style="float:initial">Remove</button>
<script type="text/javascript">
$('.Remove').click(function () {
var myId = $(this).data('id');
$.ajax({
type: "POST",
url: '@Url.Action("Remove", "ReadingNow")?Book_id=' + myId,
success: function (response) {
$('#myElem').html(response).fadeIn('slow');
$('#myElem').delay(8000).fadeOut('slow');
},
failure: function (response) {
alert("Failure");
}
});
});
</script>
and here is the function :
public class ReadingNowController : Controller
{
[HttpGet]
public ActionResult Remove(int? Book_id)
{
if (User.Identity.IsAuthenticated)
{
var x = User.Identity.GetUserId();
var IsExists = db.ReadingNow.Where(t => t.Book_Id == Book_id && t.User_Id == x).FirstOrDefault();
if (IsExists != null)
{
db.ReadingNow.Remove(IsExists);
int state = db.SaveChanges();
if (state == 1)
{
return Content("Book Removed From Your Reading Now List !");
}
}
}
return Content("Error !");
}
}
Note: When I tried to call it directly it works, but when used ajax I got no result... How can I solve this problem?