Mvc Jquery Ajax Post returns null
Solution 1:
If you serializing the form, then you can add additional values to it with the .param()
function
var data = $("#RentForm").serialize() + '&' + $.param({ 'aracid': AracID }, true);
$.ajax({
type: "POST",
url: '@Url.Action("Save","AracKirala")',
data: data,
....
Solution 2:
MVC will map the object for you, so you might as well skip the extract nesting of the form within the object.
Notes:
- If
aracid
is also a property in the model, it will map to both the property and the extra parameter. - Using push on the
serialise()
collection is more maintainable than the alternative of concatenating strings before theserialize()
call.
e.g.
var Kiralayan = $("#RentForm").serialize();
// Add the extra non-form parameter
Kiralayan.push({name: 'aracid', value: P.AracID});
Full example:
function Kaydet() {
var Kiralayan = $("#RentForm").serialize();
// Add the extra non-form parameter
Kiralayan.push({name: 'aracid', value: P.AracID});
console.log(params);
$.ajax({
type: "POST",
url: '@Url.Action("Save","AracKirala")',
data: Kiralayan,
dataType: "text",
success: function (response) {
if (response != "OK") {
alert("Kayıt yapılamadı.");
}
else {
document.getElementById("RentForm").reset();
alert("Kayıt başarıyla gerçekleştirildi.");
$("#myModal").modal('hide');
Ara();
}
}
});