Spud
Spud

Reputation: 243

How to generate angularjs model from c# class

The angular app I am working on has several large input forms which span several pages. The data is added to an angular model which is a javascript object literal and sent to a webApi controller. The webApi parameter for my POST methods is a C# class (duh) with a lot of properties! Is there a utility which will generate the javascript from my C# class, so that my binding just works! I've googled this and failed even though it seems such a mundane task. As always thanks in advance.

Upvotes: 2

Views: 2231

Answers (1)

Or Guz
Or Guz

Reputation: 1018

You should use JSON serialize on your JS side and deserialize on your c# side.

JS:

System.Web.Script.Serialization.JavaScriptSerializer serializer = 
new System.Web.Script.Serialization.JavaScriptSerializer();

string jsonParam = oSerializer.Serialize(param);

While on your c# side you should use something like, lets say your class is Person:

C#:

Person person = new JavaScriptSerializer().Deserialize<Person>(param);

see msdn documentation on serialize\deserialize json object

Upvotes: 0

Related Questions