[Solved] Button Click event not firing when post data using Ajax in ASP.Net Core MVC

trisetia302
 
on Sep 14, 2021 10:52 PM
327 Views

Hi,

I will save data using Ajax but when the button click not firing, I tried to solve but I didn't get the solution. When I test outside form the button firing.

Anny help could be appreciate.

The View

<script src="~/lib/jquery/dist/jquery.js"></script>
<div class="row">
    <div class="col-md-12">
        <div class="card">
            <div class="card-header card-header-success">
                <h4 class="card-title">Add Pembelian Stock Obat</h4>
                <p class="card-category"></p>
            </div>
            <div class="card-body">
                <form>
                    <input asp-for="No_Pembelian_Obat" id="No_Pembelian_Obat" class="form-control" hidden="hidden" />
                    <div asp-validation-summary="ModelOnly" class="text-danger"></div>
                    <div class="row">
                        <div class="col-md-6">
                            <div class="position-relative form-group">
                                <label asp-for="Tanggal" class="control-label"></label>
                                <input asp-for="Tanggal" id="Tanggal" type="date" class="form-control" />
                                <span asp-validation-for="Tanggal" class="text-danger"></span>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="position-relative form-group">
                                <label asp-for="Nama_Karyawan" class="control-label"></label>
                                <input asp-for="Nama_Karyawan" id="Nama_Karyawan" class="form-control" />
                                <span asp-validation-for="Nama_Karyawan" class="text-danger"></span>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="position-relative form-group">
                                <label asp-for="Nama_Supplier" class="control-label"></label>
                                <input asp-for="Nama_Supplier" id="Nama_Supplier" class="form-control" />
                                <span asp-validation-for="Nama_Supplier" class="text-danger"></span>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div align="center">
                                <div class="form-group">
                                    <button type="button" id="BtnSavePembelianStockObat" class="btn btn-success"> Save</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

The Ajax

<script type="text/javascript">
    $(document).ready(function(){
        $("#BtnSavePembelianStockObat").click(function () {
            var ObjectPembelian = {
                No_Pembelian_Obat: $("#No_Pembelian_Obat").val(),
                Tanggal: $("#Tanggal").val(),
                Nama_Karyawan: $("#Nama_Karyawan").val(),
                Nama_Supplier: $("#Nama_Supplier").val()
            };
            $.ajax({
                url: "/PembelianStockObat/SavePembelianObat",
                data: JSON.stringify(ObjectPembelian),
                type: "POST",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (result) {
                    window.alert("Data berhasil disimpan..");
                },
                error: function (errormessage) {
                    window.alert(errormessage.responseText);
                }
            });
            return false;
        });
    });
</script>

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
trisetia302
 
on Sep 15, 2021 02:37 AM

Problem Solved Sir,,

When I restrart my laptop and then rebuild my project the button click working properly.