[SOLVED] Call Partial View with Modal using onClick in a button ASP.NET mvc

Issue

I want from a button to call a partial view using that has a modal using onClick for some reason it’s not working the function.

Let me know what I can change to make it work!.

Button

<a class="btn btn-outline-info mx-2 mx-sm-1" onclick="showModal()" id="nav-btn" ><i class="bi bi-upc-scan"></i></a>

Script

  <script>
        function showModal() {
            $("#div1").load("/PurchaseOrder/Barcode",
                function () { $("#livestream_scanner").modal('toggle'); });
        }
    </script>

Controller


 public ActionResult Barcode()
        {
            return PartialView("~/Views/Barcode/SearchScanner.cshtml");
        }

View with Modal

<div class="modal" id="livestream_scanner" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Search Barcode Scanner</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <div id="qr-reader" style="width:450px"></div>


                <div id="qr-reader-results" style="margin-bottom: 25px;"></div>
               
                </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

Solution

Example:

<div class="modal" id="livestream_scanner" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Search Barcode Scanner</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body" id="modal-body">
                <div id="qr-reader" style="width:450px"></div>


                <div id="qr-reader-results" style="margin-bottom: 25px;"></div>
               
                </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

Then function

function showModal() {

    $.ajax({
        url: "/PurchaseOrder/Barcode",
        dataType: 'html',
        beforeSend: function () {

        },
        success: function (data) {

            $('#modal-body').html(data);
            $('#livestream_scanner').modal('show');
        }
    });
}

Answered By – Adlorem

Answer Checked By – Gilberto Lyons (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *