我正在使用.Net MVC和Entity Framework。 在我的Model Class中,我有以下两个属性:
public string Content { get; set; }
[NotMapped]
public dynamic DynamicContent { get { return Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(this.Content); } }
“Content”包含JSON字符串,DynamicContent是基于JSON字符串的动态属性。
我可以修改动态属性的内容吗? 例如:我可以读取这样的值
DynamicContent.title
但如何从控制器设置其值?
DynamicContent.title = "myvalue"
不起作用。
答案 0 :(得分:1)
您应该只能设置Content属性的值,因为这是调用get方法时DynamicContent属性检索的值。
所以而不是:
DynamicContent.title = "myvalue"
你会打电话:
Content = *the json representation of the content*
然而,这需要采用JSON格式,因为DynamicContent getter会从JSON中对其进行DeSerializes。