Hi Good Peoples,
I was tried to save data using ajax but always get the error message like this "NullReferenceException: Object reference not set to an instance of an object".
Before I'm ever get the error message like that, when I added "[FromBody]" on headed controller the problem solved but now I do same way the problem not solved.
I was tried to solve this issue but I can't solve it yet.
I sure the value on the dropdownlist is not null. Where is the mistake in my code?
Any help could be apriciate.
This is the log of error message
NullReferenceException: Object reference not set to an instance of an object.
Penginapan_Apps.Controllers.PesananKamarController.UpdateStatusKamar(PesananKamarModel pesanan) in PesananKamarController.cs
The Controller
[HttpPost]
public JsonResult UpdateStatusKamar([FromBody] PesananKamarModel pesanan)
{
int i;
using(SqlConnection con = new SqlConnection(this._configuration.GetConnectionString("Penginapan_Apps")))
{
using(SqlCommand cmd = new SqlCommand("UpdateStatusKamar"))
{
con.Open();
cmd.Connection = con;
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@ID_Kamar",Convert.ToInt32(pesanan.ID_Kamar));
cmd.Parameters.AddWithValue("@Status", pesanan.Status);
i = cmd.ExecuteNonQuery();
TempData["pesan"] = "Data berhasil disimpan..";
}
}
return Json(i);
}
The Ajax
<script type="text/javascript" lang="javascript">
$(document).ready(function () {
$('#BtnSimpanPesananKamar').on("click", function () {
var Status = {
ID_Kamar: $("#DropdownListNomerKamar").val(),
Status: $("#Status").val("Room Was Reservation")
};
$.ajax({
url: "@Url.Action("UpdateStatusKamar", "PesananKamar")",
data: JSON.stringify(Status),
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function () {
MessageSuccess();
},
error: function (errormessage) {
window.alert(errormessage.responseText);
}
});
});
});
function MessageSuccess() {
Swal.fire({
position: 'top',
icon: 'success',
title: 'Good job!',
text: 'Data berhasil disimpan...'
});
}
</script>