Do not reload page when giving Submit in modal

Asked

Viewed 573 times

1

I have a modal to research customers: inserir a descrição da imagem aqui

Which is called a registration form as follows:

$('#linkSearchShipperCustomer').on("click", function (e) {
            showDialogList("@Url.Action("IndexShadow", "ShipperCustomer")", "IdShipperCustomerDiv", "IdShipperCustomer", "ShipperCustomerFullName", "Buscar Cliente", function (value, text) {
                loadCustomerAddress(value);
            });

        });

the showDialog has that body:

function showDialogList(url, idDiv, idValueUpdate, idTextUpdate, dialogTitle, callback) {
debugger;
elementChoosedCallback = callback;

idElementValueUpdate = idValueUpdate;
idElementTextUpdate = idTextUpdate;

$.post(url, {}, function (result) {
    $("#" + idDiv).html(result);
    if (dialogTitle != undefined)
        $("#" + idDiv).dialog({ width: 670, title: dialogTitle });
    else
        $("#" + idDiv).dialog({ width: 670 });

  });
}

and calls this action:

public ActionResult IndexShadow(ShipperCustomerFilter filter)
    {
        filter.IsActive = true;
        filter.PageSize = 12;

        var modelList = new ShipperCustomerListViewModel()
        {
            ShipperCustomers = ShipperCustomerViewModel.ConvertFromModelList(repository.GetAllShipperCustomers(filter, out totalRecords)),
            LegalTypes = GetAllLegalTypes()
        };

        CreateRouteValuesForFilters();

        ViewBag.PagingModel = SetCurrentPage(totalRecords, filter.Page, filter.PageSize, true);
        ViewBag.PagingModel.FormId = "ShipperCustomerFormSearchId";

        return View(modelList);
    }

What I think is the problem is, when I give Submit in modal it reloads the back page and returns like this, reloaded the father with the fields of modal:inserir a descrição da imagem aqui

how to do not reload the page that called the modal, and display the results in the modal itself? (it is not in the image but it returns the results on this wrong page).

EDIT: After @Harrison’s tip, I saw that my code does not enter the function calling the e.preventDefault();I don’t know why.

$("#ShipperCustomerFormSearchId").on("submit", function (e) {
        debugger;
        e.preventDefault();

        $.post($(this).attr("action"), $(this).serialize(), function (result) {
            $("#IdShipperCustomerDiv").html(result);
        });
    });`

my beginForm:

@using (Html.BeginForm("IndexShadow", "ShipperCustomer", ViewBag.Routes as RouteValueDictionary, FormMethod.Get, new { @id = "ShipperCustomerFormSearchId", @class = "middle-forms" }))

1 answer

2

To cancel a form Ubmit you have to use the preventDefault in the event, for example:

$('form').on('submit', function(e) {
    e.preventDefault();
    // insira aqui sua chamada ajax
})
  • the problem is that if I do that the form itself Ubmit will not work, my problem is to reload the father when I give Ubmit on the son, ie, he reloads the page that opened the modal when I give Submit on modal.

  • you were right there already exists a function with e.preventDefault();, but my code does not enter it. I edited the question.

  • use the console’s Inspect to confirm that the id #ShipperCustomerFormSearchId is inserted into the tag <form>, Since I don’t know how your income is done, it may be that this id enters a div, for example: <div id="ShipperCustomerFormSearchId"><form>...</form></div>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.