Search code examples
javascriptc#asp.netasp.net-web-api

How to construct a object in JS which corresponds to an object in C#


I use .Net web API and I want to get the structure of my objects in C# and send it to the front so it can construct object exactly like the back.

I don't know how to get the exact structure of my objects in C# (the structure and the name of the attributes).

For example, an object will be this kind of object:

public class{
  public int a;
  private string b;
  private anAnotherObject c;
  private List<otherObjects> d;
}

Solution

  • First why you are having private members?

    Let's say if they are public. Then you can use Javascript Serializer to serialize you object to json and send it to html or View.

    Using ajax call you can send json that will be received as object on ur action method

      $.ajax({url:url,
              data:{j:dataModelOnJson},...})
    

    })