Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic object and AJAX call mvc

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.

like image 820
Quoter Avatar asked Dec 02 '25 11:12

Quoter


1 Answers

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

like image 79
drizzie Avatar answered Dec 05 '25 02:12

drizzie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!