How to do an AJAX call to a MVC Controller that expects a dynamic object?
This was my try:
public JsonResult Test(dynamic someObject)
{
return;
}
$.ajax({
type: 'GET',
url: '/home/test',
data: JSON.stringify( { test: 1, test2: 2 } ),
contentType: 'application/json',
success: function (data) {
alert(data);
},
error: function (error, data) {
alert("something went wrong: " + error );
}
});
But I get just {object} as a value in someObject.
This isn't possible because MVC cannot deserialize an object that it doesn't know a type for.
I recommend passing the data in as a JSON string and then deserializing that.
public JsonResult Test(string someObject)
{
dynamic y = new JavaScriptSerializer().Deserialize<dynamic>(someObject);
return;
}
JavaScriptSerializer can be found in System.Web.Extensions
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With