1

I have a model like so in javascript:

function SomeModel()
{
   this.Id = ko.observable();
   this.Name = ko.observable();
   this.MetaData = {};
}

Then I need to send this to MVC so I can persist it in mongodb, however I dont know how is best to store the MetaData in the C# representation... as the model looks like:

public class SomeModel
{
   public Guid Id {get;set;}
   public string Name {get;set;}
   public object MetaData {get;set;}
}

However it doesnt like binding the json object to the object, I am not sure if I should be using an ExpandoObject or something, as I am not really that fussed about accessing the data within the c# mvc service, it is only for plugins in the front end which can store their own data on an object for use later. So an example could be:

var someModel = new SomeModel();
someModel.MetaData.Bookmarks = [];
someModel.MetaData.Bookmarks.push({ location: 120, document: 23003032 });

So it could contain anything and sub objects, and like I say I just need to bung it into an object just as a transport mechanism for Mongo, which will save it fine as its schema-less.

Currently the models from JS are sent over ajax via Jquery, and auto binded in the controller then sent off to the repository.

== Edit ==

I have done a few minor tests with MVC4, and I can put an expando object within my models and have it populated, however it only seems to store 1 layer of data, so if I were to postback:

...,
MetaData: {
   something: {
       one: 1,
       two: 2 
   }
},
...

I would end up with my expando object for the MetaData field knowing about the something field, but would not store any of the data below that first traversal, so is there any way to store the above other than turning it into a string of json to be stored?

1 Answer 1

1

As there seemed to be no solid answers I just had to do an extra step in the jquery ajax to convert all "metadata" fields into a json string, then store it as a string on the server.

Then if I need to access or change the data I use the web helper Json.Encode/Decode which returns a dynamic, then I can just add child elements which I do as:

  • Anonymous type for simple included json object
  • List for an array element

Then I just encode it again casting the dynamic as an object.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.