<a> click event not firing from jquery

时间:2016-01-27 16:05:40

标签: jquery asp.net-mvc asp.net-ajax

I have a requirement to update data without redirecting to another page. In my controller I have a method with a return type of void which updates the data. In my partial view I have an <a> element. On click of the link I am calling the above method which returns void to my jQuery file.

<div id="Footer">
    <a href="#" id="MarkAllRead">Mark All Read</a>
</div>
$(".MarkAllRead").click(function () {
    alert('click');
    $.ajax({
        url: "/Controller/MarkNotification",
        method: 'POST',
        cache: false,
        datatype: "text",
        success: function (result) {
            $("#ncount").hide();
        },
        error: function (xhr) {
            alert(result.responseText);
        }
    });
});
public void MarkNotificationAsRead()
{
    db.Notifications.ToList().ForEach(n => n.Status = true);
    db.SaveChanges();
}

The problem I am facing is that the AJAX call is not getting fired. On click of the link I want to call the above method which returns void from my jQuery file.

2 个答案:

答案 0 :(得分:1)

当元素具有class时,您将id选择。改变这个:

$(".MarkAllRead").click(function () {

到此:

$("#MarkAllRead").click(function () {

答案 1 :(得分:1)

我认为你的jQuery选择器有错误。

$(".MarkAllRead").click(function () {

应该变成这样:(使用#而不是点 - 点用于类nd#是用于id)

$("#MarkAllRead").click(function () {