skip to Main Content

I can’t send the data to the controller with ajax. goes "null". please help me.

my html codes:

<div class="modal fade" id="sagTikMenuKategoriGuncelleModal" data-bs-backdrop="static">
                <div class="modal-dialog modal-dialog-centered">
                    <div class="modal-content">
                        <div class="modal-header" style="font-weight: 600; color: rgb(157, 163, 173); background-color: rgb(248, 248, 248);">
                            <h5 class="modal-title">Kategori Alanı Güncelle</h5>
                            <button class="btn-close" data-bs-dismiss="modal"></button>
                        </div>
                        <div class="modal-body">
                            <div class="col-md-12 pb-4 text-center kategoriAlanlariModalBodyDiv" style="font-weight: bold; color:rgb(168, 174, 185);">
                                <span>Kategori alanınınız adını buradan güncelleye bilirsiniz..</span>
                            </div>
                            <form class="needs-validation" novalidate id="sagTikMenuKategoriGuncelleModalForm">
                                <div class="row g-3">
                                    <div class="col-md-12">
                                        <label for="kategoriAlanGuncelleAdi" class="form-label">Kategori Alan Adı *</label>
                                        <input id="kategoriAlanGuncelleAdi" type="text" class="form-control" placeholder="Kategori Alan Adı" required>
                                        <div class="invalid-feedback">
                                            Bu Alan Boş Geçilemez
                                        </div>
                                    </div>
                                </div>
                                <div class="modal-footer mt-3">
                                    <button class="btn btn-secondary" type="submit" id="sagTikMenuKategoriGuncelleButonModal">Güncelle</button>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>

my jquery codes:

var kategoriAlanGuncelleId;
    var kategoriAlanGuncelleAd;

$(".urunKategorileriAlanAdlari > span").contextmenu(function () {
        kategoriAlanGuncelleId = $(this).attr("id");
        kategoriAlanGuncelleAd = $(this).text();
    });

$("#sagTikMenuKategoriGuncelleButonModal").click(function () {
        var kategoriAlanAdGuncel = new Object();
        kategoriAlanAdGuncel.urunKategorileriKategoriAdi = $("#kategoriAlanGuncelleAdi").val();
        kategoriAlanAdGuncel.urunKategorileriId = kategoriAlanGuncelleId;


        var kategoriAlanGuncelAdres = "@Url.Action("kategoriAlanGuncelle", "kategoriAlanlari")";

        $.ajax({
            url: kategoriAlanGuncelAdres,
            type: "POST",
            data: kategoriAlanAdGuncel,
            success: function (veri) {
              
            },
            error: function (hata, ajaxoptions, throwerror) {
                alert("Hata :" + hata.status + " " + throwerror + " " + hata.responseText);
            }
        });
    });

c# controller:

[HttpPost]
        public IActionResult kategoriAlanGuncelle(urunKategorileri mod)
        {
            if (mod == null)
            {
                return RedirectToAction("Kategoriler", "Home");
            }

            if (mod.urunKategorileriId == 0 || mod.urunKategorileriKategoriAdi == null)
            {
                return RedirectToAction("Kategoriler", "Home");
            }

            var veri = kategoriAlanlariServices.kategoriGuncelle(mod);

            if (veri == 0)
            {
                return RedirectToAction("Kategoriler", "Home");
            }

            return RedirectToAction("Kategoriler", "Home");
        }

I checked the data I received in jquery with "alert". they are full. but still null value goes to controller. The derivatives of the object variable I created in jquery are the same as the table columns.I couldn’t find the problem

4

Answers


  1. Try explicitly setting contentType and datatype eg.

    contentType: 'application/json',
    dataType: 'json',
    
    Login or Signup to reply.
  2. Try this:

    $.ajax({
        url: kategoriAlanGuncelAdres,
        type: "POST",
        data: { "mod": kategoriAlanAdGuncel },
        success: function (veri) {
            
        },
        error: function (hata, ajaxoptions, throwerror) {
            alert("Hata :" + hata.status + " " + throwerror + " " + hata.responseText);
        }
    });
    
    Login or Signup to reply.
  3. The answers of Alexander Solonik and Chris Wong are both correct. And I also check your code in my local, and it works fine. And I not use your code about modal.

    enter image description here

    I’m not sure where the problem occurred, so I can only give you general advice. Then I determined that the problem may appear in the form in the modal, you need to locate the problem through the following steps.

    Steps

    1. Create a div outside of modal, and then create a button click event. You can write the value of urunKategorileri_Model as a fixed value. Other codes are the same for testing.

    2. If sucess, then the explanation is related to modal.

    3. Follow below steps.

      enter image description here

    4. Press F12, check the details about the request.

      enter image description here

    5. If you got 404, it means your request url is wrong. If you get 500, it means there are some error when your webapp is running.

    6. If you got 200, you can debug your C# code.

      enter image description here

    Login or Signup to reply.
  4. Your code is true and I just copied it and it works but I think ur problem is in your controller class just make ur properties data type right like mine

      public class urunKategorileri
        {
            public string urunKategorileriKategoriAdi { get; set; }
            public int urunKategorileriId { get; set; }
        }
    

    my jquery version is v3.5.1

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search