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
Try explicitly setting
contentType
anddatatype
eg.Try this:
The answers of
Alexander Solonik
andChris 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.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
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.
If sucess, then the explanation is related to modal.
Follow below steps.
Press F12, check the details about the request.
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.
If you got 200, you can debug your C# code.
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
my jquery version is v3.5.1