I have the following arbitrary JSON object(The field names may be changed).
{
firstname: "Ted",
lastname: "Smith",
age: 34,
married : true
}
-
public JsonResult GetData(??????????){
.
.
.
}
I know that I can define a class just like the JSON object with the same field names as the argument, But I would like my controller to accept arbitrary JSON object with different field names.
If you want to pass custom JSON object to MVC action then you can use this solution, it works like a charm.
The real benefit of this solution is that you don't require to define a new class for each combination of arguments and beside that, you can cast your objects to their original types easily.
UPDATED
Now, you can even merge your GET and POST action methods since your post method doesn't have any argument any more just like this :
and you can use a helper method like this to facilitate your job
you can also use this in MVC 4
Have a ViewModel with the same signature and use that as the argument type.Model binding will work then
And your Action method will look like