简体   繁体   English

将带有动态json对象的模型传递给MVC控制器

[英]Passing model with dynamic json object to MVC controller

I have a model like so in javascript: 我在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: 然后我需要将其发送到MVC,以便可以将其持久保存在mongodb中,但是我不知道如何最好地将MetaData存储在C#表示中……因为模型看起来像这样:

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. 但是它不喜欢将json对象绑定到该对象,我不确定是否应该使用ExpandoObject之类的东西,因为我对访问c#mvc服务中的数据并不是很着迷,它仅适用于可以将自己的数据存储在对象上以供以后使用的前端。 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. 因此它可以包含任何内容和子对象,就像我说的那样,我只需要将其绑定到对象中,就像Mongo的传输机制一样,它将节省它的较少架构。

Currently the models from JS are sent over ajax via Jquery, and auto binded in the controller then sent off to the repository. 当前,来自JS的模型通过Jquery通过ajax发送,并在控制器中自动绑定,然后发送到存储库。

== 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: 我已经使用MVC4做了一些次要测试,可以在模型中放置一个expando对象并填充它,但是它似乎只存储1层数据,所以如果要回发:

...,
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? 我将以Metadata字段的expando对象最终了解something字段,但不会在第一次遍历以下存储任何数据,因此除了将其转换为json字符串之外,有什么方法可以存储上面的数据被存储?

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. 由于似乎没有确凿的答案,我只需要在jquery ajax中执行一个额外的步骤即可将所有“元数据”字段转换为json字符串,然后将其作为字符串存储在服务器上。

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: 然后,如果需要访问或更改数据,可以使用Web助手Json.Encode/Decode返回动态信息,然后可以添加子元素,其操作如下:

  • Anonymous type for simple included json object 简单包含的json对象的匿名类型
  • List for an array element 数组元素列表

Then I just encode it again casting the dynamic as an object. 然后,我再次对其进行编码,将动态对象转换为对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM