简体   繁体   中英

Is there a possibility to bind the Form to a modal bootstrap window without using the load call?

I'm using ajax to make a request and open a modal bootstrap window afterwards. The problem is that when I use ajax, I make a request to my controller, and the return (modal content) I load as follows:

//modal loading
$('#contentModalFinanceiroParcela').html(data);

//exibição da modal
$('#modalFinanceiroParcela').modal({
    keyboard: true,
}, 'show');

So far, everything perfect. The problem is that from then on, I can't bind the form to register the submit event of the form. In the function bindFormFinanceiroParcela, no matter how much I pass the "dialog", bind does not work.

bindFormFinanceiroParcela(document.getElementById("contentModalFinanceiroParcela"));

Searching the forums, I found that the process works if I load the modal using the "load" command, as below, but I can't do it like that, otherwise it will make a second request to the controller, because previously, I already used ajax.

//That way it works, but I can't use it.
$('#contentModalFinanceiroParcela').load(url, function () {
    $('#modalFinanceiroParcela').modal({
        keyboard: true
    }, 'show');

    // Inscreve o evento submit
    bindFormFinanceiroParcela(this);

    stopLoadPage();
});

Is there a possibility that I can bind the form without using the "load" command mentioned in the script above?

    function openModalFinanceiroParcelaSemURL(data) {
    startLoadPage();

    //Create the modal window block in the body of the page
    if (!$("#modalFinanceiroParcela").data('bs.modal'))
        CreateModalFinanceiroParcela();

    //Load modal content via ajax request
    $('#contentModalFinanceiroParcela').html(data);

    $('#modalFinanceiroParcela').modal({
        keyboard: true,
    }, 'show');

   bindFormFinanceiroParcela(document.getElementById("contentModalFinanceiroParcela"));

   
    
    stopLoadPage();
}

function bindFormFinanceiroParcela(dialog) {
$('form', dialog).submit(function (e, i) {
    if ($(this).valid() || i) {
        startLoadOneMoment();
        $.ajax({
            url: this.action,
            type: this.method,
            data: $(this).serialize(),
            success: function (result) {
                if (result.success) {
                     window.location = window.location;                       
                } else {
                    $('#contentModalFinanceiroParcela').html(result);
                    bindFormFinanceiroParcela();
                }
                stopLoadOneMoment();
            }
        });
        return false;
    } else {
        return false;
    }
});

function CreateModalFinanceiroParcela() {
var html = '<div class="modal modal-primary modal-system" tabindex="-1" role="dialog" id="modalFinanceiroParcela" data-backdrop="static"><div class="modal-dialog modal-dialog-centered"><div class="modal-content"><div class="content-modal-system" id="contentModalFinanceiroParcela"></div></div></div></div>';

$("body").append(html);
}

RAZOR DELETE:

@using Retaguarda.Domain.Enuns
@model Retaguarda.Application.ViewModels.Financeiro.FinanceiroParcela.FinanceiroParcelaViewModel
@{
    ViewData["Title"] = "Excluir Parcela";
    Layout = null;
}
<div>
    <form asp-action="Delete" id="frm-excluir-financeiro-parcela">
        @Html.AntiForgeryToken()

        <div class="modal-shadow">
            <div class="modal-header modal-header-primary">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
                <h4><i class="modal-title text-center glyphicon glyphicon-trash"></i> @ViewData["Title"] </h4>
            </div>
            <div class="panel">
                <div class="panel-body  container-fluid pt-15 pl-15 pr-15">
                    <div class="form-horizontal">
                        <vc:summary />
                        <br />
                        <div class="message-delete">
                            @Html.HiddenFor(model => model.Id, new { id = "hid-financeiro-parcela-id" })
                            <i class="icon fa-trash" aria-hidden="true"></i>
                            <p>
                                Tem certeza de que deseja excluir a parcela @(Model.Parcela)?<br />
                            </p>
                        </div>
                    </div>
                </div>
            </div>

            <div class="modal-footer">
                <div class="col-md-offset-2 col-md-10">
                    <div class="float-right">
                        <div class="btn-group btn-group-sm mr-auto"
                             role="group">
                            <div class="btn-group btn-group-sm" role="group">
                                @*<button id="btn-excluir-financeiro-parcela" type="submit" class="btn btn-success"><i class="icon wb-check"></i> Excluir </button>*@
                                <button id="btn-excluir-financeiro-parcela" type="button" class="btn btn-success"><i class="icon wb-check"></i> Excluir </button>
                                <button id="btn-cancelar-financeiro-parcela" class="btn btn-danger" data-dismiss="modal"><i class="icon wb-close"></i> Cancelar </button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>
</div>

<script src="~/lib/jquery-validation/dist/jquery.validate.js"></script>
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js"></script>


@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

Ajax call

$('#dtFinanceiroParcela').on('click', 'tr .btn-excluir-financeiro-parcela', function (e) {
e.preventDefault();

startLoadOneMoment();

var id = $(this).attr('data-id');
var data = { id: id };
var dataURL = jQuery.param(data);

$.ajax({
    url: "/financeiro-parcela-gerenciar/remover-financeiro-parcela/" + id,
    type: "GET",
   // data: dataURL,
    contentType: "application/json",
    async: false,
    success: function (result) {
        if (typeof result.success !== 'undefined') {
            if (!result.success) {
                stopLoadOneMoment();

                swal("Oops", result.message, "error");

                return false;
            }
        }
       // alert(this.url);
        stopLoadOneMoment();
        openModalFinanceiroParcelaSemURL(result);
        
        return false;
    },
    error: function () {
        stopLoadOneMoment();
        alert("Oops! Algo deu errado.");
        return false;
    }
});

Your form inside razor does not contain any submit button because its commented out.

@*<button id="btn-excluir-financeiro-parcela" type="submit" class="btn btn-success"><i class="icon wb-check"></i> Excluir </button>*@

Remove the comment or change the type of the other button to "submit"

I guess the submit event is attached successfully but never called due to the missing submit button inside your form.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM