title: How to post a dynamic JSON property to a C# ASP.Net Core Web API using MongoDB?
body: <p>How to post a dynamic JSON property to a C# ASP.Net Core Web API using MongoDB?</p>
  
  <p>It seems like one of the advantages of MongoDB is being able to store anything you need to in an Object type property but it doesn't seem clear how you can get the dynamic property info from the client ajax post through a C# Web API to MongoDB.</p>
  
  <p>We want to allow an administrator to create an Event with Title and Start Date/Time but we also want to allow the user to add custom form fields using Reactive Forms for whatever they want such as t-shirt size or meal preference...  Whatever the user may come up with in the future.  Then when someone registers for the event, they post EventID and the custom fields to the Web API.</p>
  
  <p>We can have an Event MongoDB collection with _id, event_id, reg_time, and form_fields where form_fields is an Object type where the dynamic data is stored.</p>
  
  <p>So we want to POST variations of this JSON with custom FormsFields:</p>
  
  <p>Variation 1:</p>
  
  <pre><code>{
      "EventId": "595106234fccfc5fc88c40c2",
      "RegTime":"2017-07-21T22:00:00Z",
      "FormFields": {
          "FirstName": "John",
          "LastName": "Public",
          "TShirtSize": "XL"
      }
  }
  </code></pre>
  
  <p>Variation 2:</p>
  
  <pre><code>{
      "EventId": "d34f46234fccfc5fc88c40c2",
      "RegTime":"2017-07-21T22:00:00Z",
      "FormFields": {
          "Email": "John.Public@email.com",
          "MealPref": "Vegan"
      }
  }
  </code></pre>
  
  <p>I would like to have an EventController with Post action that takes a custom C# EventReg object that maps to the JSON above.  </p>
  
  <p>EventController:</p>
  
  <pre><code>[HttpPost]
  public void Post([FromBody]EventReg value)
  {
      eventService.AddEventRegistration(value);
  }
  </code></pre>
  
  <p>EventReg Class:</p>
  
  <pre><code>public class EventReg
  {
      public EventReg()
      {
          FormFields = new BsonDocument();
      }
      [BsonId]
      [BsonRepresentation(BsonType.ObjectId)]
      public string EventRegId { get; set; }
  
      [BsonElement("EventId")]
      [BsonRepresentation(BsonType.ObjectId)]
      public string EventId { get; set; }
  
      [BsonElement("reg_time")]
      public DateTime RegTime
      {
          set; get;
      }
  
      [BsonElement("form_fields")]
      public MongoDB.Bson.BsonDocument FormFields { get; set; }
  }
  </code></pre>
  
  <p>EventService   </p>
  
  <pre><code>public string AddEventRegistration(EventReg eventReg)
  {
      this.database.GetCollection&lt;EventReg&gt;("event_regs").InsertOne(eventReg);
      return eventReg.EventRegId;
  } 
  </code></pre>
  
  <p>Right now, if I post to the controller, my EventReg is null because it must not know how to map my JSON FormFields properties to a BsonDocument.</p>
  
  <ul>
  <li>What type can I use for FormFields? </li>
  <li>Can I have the FormFields property be a BsonDocument and is there an easy way to map the Web API parameter to that?</li>
  <li>Is there an example of how some custom serializer might work in this case?</li>
  </ul>
  
  <p>We could maybe use a dynamic type and loop through the posted properties but that seems ugly.  I have also seen the JToken solution from a post here but that looks ugly also. </p>
  
  <p>If MongoDB is meant to be used dynamically like this, shouldn't there be a clean solution to pass dynamic data to MongoDB?  Any ideas out there?</p>