user1024941 user1024941 - 2 months ago 8x
C# Question

Passing Angular Formly model to c# Controller

I have an AngularJS app that is using formly to generate a form. Below is an example of the model data.

"form_field_17": "None Required",
"title": 1,
"firstName": "John",
"lastName": "Doe",
"email": "",
"mobile": "1234567890",

What kind of object should I use in my C# controller to receive this? I can't have a pre-defined class, as the controller should be able to accept any type of json data.


if you really want to be able to accept ANY type of valid JSON (and there are probably good business and security rules for NOT doing this) and you want to deserialize before reaching the method (rather than accepting the raw JSON string), then you're down to two options.

JToken, if using Newtonsoft Json.NET:

public ResultType DynamicPayloadEndpoint(JToken dynamicObject)

or use the actual dynamic keyword:

public ResultType DynamicPayloadEndpoint(dynamic dynamicObject)

How you handle exposing that data at that point will always be tricky, but you'll be able to accept anything.

Keep in mind that if using ASP.NET MVC, this makes modelbinding useless.