please, I need help, why does [FromBody] fetch value always return null value. I have tried everything but nothing has worked, maybe you have a better idea? Please help, i stuck with this code
This is my javascript :
const DetailSewa = barangSewa;
const sewa = {
NotaSewa: $("#notaSewa").val(),
PelangganId: $("#cboSearchPelanggan").val(),
TempatAcara: $("#txtTempatAcara").val(),
TanggalAcara: $("#txtTanggalAcara").val(),
OngkosKerja: $("#ongkosKerja").val(),
OngkosKirim: $("#ongkosKirim").val(),
OngkosCuci: $("#ongkosCuci").val(),
SisaBayar: $("#sisaBayar").val(),
UangMuka: $("#uangMuka").val(),
TotalBayar: $("#totalBayar").val(),
TipeDokumen: $("#cboSearchDokumen").val(),
DetailSewa: DetailSewa,
};
$("#btnSimpan").closest("div.card-body").LoadingOverlay("show");
fetch("/Transaksi/CreateSewaPeralatan", {
method: "POST",
headers: { "Content-Type": "application/json;charset=utf-8" },
body: JSON.stringify(sewa),
})
.then((response) => {
$("#btnSimpan").closest("div.card-body").LoadingOverlay("hide");
return response.ok ? response.json() : Promise.reject(response);
})
.then((responseJson) => {
if (responseJson.State) {
swal("Terdaftar !", "Nota : " $("#notaSewa").val(), "sukses");
} else {
swal("Maaf", "Sewa barang gagal terdaftar", "error");
}
})
.catch((error) => {
$("#btnSimpan").closest("div.card-body").LoadingOverlay("hide");
});
my controller :
[HttpPost]
public IActionResult CreateSewaPeralatan([FromBody] Sewa model)
{
ResponseSewa<Sewa> gResponse = new ResponseSewa<Sewa>();
try
{
_transaksiService.CreateSewa(model);
gResponse.State = true;
gResponse.Object = model;
}
catch (Exception ex)
{
gResponse.State = false;
gResponse.Message = ex.Message;
}
return StatusCode(StatusCodes.Status200OK, gResponse);
}
my config Json Serializtion:
services
.AddMvc()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
options.JsonSerializerOptions.PropertyNamingPolicy = null;
});
I hope the fetch can pass the value into controller
CodePudding user response:
Why does [FromBody] fetch value always return null value. I have tried everything but nothing has worked, maybe you have a better idea? Please help, i stuck with this code.
Well, I have reeproduced your issue. You are getting null value on your controller because of your
Note: If you would like to know more details on JSON property naming policy you could check our official document here