I’m a newbie working with ajax. I have a problem while sending the data into ajax post.
The output of console.log(obj.Id)
and console.log(oke)
is 2. Then I tried to send it through data in ajax, but it end up 0 in the controller.
$(function () {
$("body").on('click', '#btnEdit', function () {
alert("clicked ok");
$("#addRowModal").modal("hide");
var obj = {};
obj.Id = $(this).attr('data-id');
oke = $(this).data("id");
console.log(obj.Id)
console.log(oke)
$.ajax({
url: '@Url.Action("Details", "InvoicePPh")',
data: oke,
type: 'POST',
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (response) {
alert("sukses");
},
error: function(response) {
alert("error")
}
});
});
});
And my controller looks like this
[HttpPost]
public JsonResult Details(int id)
{
var obj = dbContext.invoicePPhs.FirstOrDefault(s => s.Id == id);
InvoicePPh pph = new InvoicePPh();
pph2326.TaxForm = obj.TaxForm;
return Json(pph);
}
I want the ‘2’ value that passes into my controller, how can I do that? Thank you for your help.
3
Answers
Please change the data property in ajax part.
An alternative way to send your data your
Controller
method usingAjax
would be to wrap your data in aJSON
object and then send it to the server for processing. The server will be then deserialize yourJSON
object and you can access the required properties from that process:And your
Controller
method will be:If you just need id in your method parameter just change data in ajax to:
id is name of parameter from controller method.