I am trying to send a partially filled object to my controller through an Ajax post.
var child =
{
Property1: 1,
Property2: 'test',
};
$.ajax({
url: '/MyController/AddNewTest',
type: 'POST',
data: {"MyObject": child},
success: function (data) {
//Do stuff
},
error: function (data) {
//Do stuff
}
});
Controller:
[HttpPost]
public ActionResult AddNewTest(MyObject object)
{
//Do stuff
}
Object:
The object MyObject
, has a lot of other properties, but in this case, I only need Property1
and Property2
(I suspect that this may be the issue). Instead of sending two strings to my controller, I would prefer to pass this partially filled object, as this makes my life a bit easier down the road.
The problem is, that once the object arrives at my controller, everything inside the object is empty.
2
Answers
Use below in the $.ajax
due to missing the contentType it default takes
So when you receive in controller everything inside the object is empty.
Refer: https://api.jquery.com/jquery.ajax/
Or Else Pass data as
for this refer: https://www.c-sharpcorner.com/article/asp-net-mvc-jquery-ajax-form-submit-using-serialize-form-data-into-a-model/
Hope, you understand the problem.