0

After saving form data, need to load the div only not whole page refresh but it first goes to Main Page Action Controller and then the DIV Load Partial Action Controller. I am unable to find the reason why it is posting whole page.

I have added the preventDefault() command too.

$("#btnSave").click(function (e) {
    e.preventDefault();

    var url = "@Url.Action("Save", "Note")";
    var id = "1";
    var model = {
      modelfields.....
    };

    $.ajax({
        type: "POST",
        data: JSON.stringify(model),
        url: url,
        contentType: "application/json",
        success: function (data) {
            if (data == "True") {

              // Load div
                var settings = { editUrl: '@Url.Action("Get", "Note", new { ID = "-1" })' };
                 settings.editUrl = settings.editUrl.replace("-1", id);
                $("#divNoteDetails").load(settings.editUrl);
            }
            else if (data == "False") {
                alert('not saved');
            }
        },
        error: function () {
            alert('error');
        }
    });
    return false; 
});
Mahrukh Mehmood
  • 258
  • 2
  • 5
  • 17

1 Answers1

0

if your button is inside a form then its default type is submit. see the spec for details

try adding type="button" to the button, or event.preventDefault() on an event handler attached to the form itself.

chee
  • 26
  • 3