Reputation: 13
I'm scratching my head but can't find what I'm missing here. My RegionCoreModel properties always getting null.
Html:
<div>
<input type="text" id="manager_name" name="manager_name" value="">
<input type="text" id="no_of_emplyee" name="no_of_emplyee" value="">
<button id="update">Update</button>
</div>
js
$("#update").on("click", function() {
var param = JSON.stringify({
RegionCoreModel: {
"Manager": $("#manager_name").val(),
"NoOfEmployee": $("#no_of_emplyee").val()
}
});
$.ajax({
url: '@Url.Action("UpdateRegion", "Store")',
type: "POST",
data: param,
contentType: 'application/json'
}).success(function (response) {
console.log(response);
}).error(function (response) {
console.log(response);
});
});
Controller
[HttpPost]
public JsonResult UpdateRegion(RegionCoreModel region)
{
return Json(new{ manager= region.Manager, noofemployee = region.NoOfEmployee}, "text/html");
}
Upvotes: 1
Views: 142
Reputation: 36594
You don't the RegionCoreModel
wrapper, and you probably don't need to convert to JSON yourself as well.
Try this:
var param = {
"Manager": $("#manager_name").val(),
"NoOfEmployee": $("#no_of_emplyee").val()
};
Another thing to try is setting dataType
explicitly, see last paran in below call:
$.ajax({
url: '@Url.Action("UpdateRegion", "Store")',
type: "POST",
data: param,
contentType: 'application/json',
dataType: 'json'
}).success(function (response) {
console.log(response);
}).error(function (response) {
console.log(response);
});
});
Upvotes: 0
Reputation: 1911
This should work.
var param = JSON.stringify({
region: {
"Manager": $("#manager_name").val(),
"NoOfEmployee": $("#no_of_emplyee").val()
}
});
Upvotes: 2
Reputation: 4054
Try taking the properties out of quotes, like this:
var param = JSON.stringify({
RegionCoreModel: {
Manager: $("#manager_name").val(),
NoOfEmployee: $("#no_of_emplyee").val()
}
Upvotes: 0