MVC 3 AJAX Post Two Times Feature

I am using Visual Studio 2010 and MVC 3 with AJAX / JQuery. The problem I am facing is that the code sends something twice. This only happens when you post something, and then do it again.

Here is my JS:

$(document).ready(function () {
   BindEvents();
});

function BindEvents() {
$('#OpenCommentDialog').click(function (event, ui) {
    var id = $(this).attr('data');
    $('#NewCommentDialog').dialog({
        open: function (event, ui) {
            $('#BugID').val(id);
            var form = $('form', '#NewCommentDialog');
            form.submit(function (e) {
                var comment = form.serialize(true);
                CreateComment(comment);
                return false;
            });
        }
    });
    $('#NewCommentDialog').dialog('open');
    return false;
});
}

function CreateComment(comment) {
if (comment != null) {
    $.post('/Comments/Create/', comment, function (data) {
        if (data == 'Success') {
            var id = $.parseQuery(comment);
            GetComments(id.BugID);
            $('#NewCommentDialog').dialog('close');
        }
    });
}
}

function GetComments(id) {
$('#Comments').find('tr').remove();
$.getJSON('/Comments/GetComments', { id: id }, function (data) {
    if (data != null) {
        if (data.length > 0) {
            $('#CommentListTemplate').tmpl(data).appendTo('#Comments');
        }
    }
});
}

My HTML page has a jQuery dialog that calls the CreateCommentPartial.cshtml file. When the user fills it, he calls the CreateComment function, which then calls GetComments and updates the comments after the publication of a new one.

, . " ", , "CreateComment" , "GetComments" . Firebug .

? (F5) , . . , .

+3
2

script? script, ​​ . - script , , script ( script ). , script .js

+1

, , switch, .

:

 var $isActualizaComentarioEventoAsignado = false;

 if (!$isActualizaComentarioEventoAsignado) {
                $isActualizaComentarioEventoAsignado = true;
                $('#pepe').bind('clic', function(e) {});
}
0

Source: https://habr.com/ru/post/1794742/


All Articles