0

I am using .Net MVC with Entity Framework. In my Model Class I have these 2 properties:

public string Content { get; set; }

[NotMapped]
public dynamic DynamicContent { get { return Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(this.Content); } }

the "Content" contains a JSON string and the DynamicContent is a dynamic property based on the JSON string.

Can I modify the contents of the dynamic property? For example: I can read a value like this

DynamicContent.title

but how can I set its value from the controller? DynamicContent.title = "myvalue" does not work.

2
  • Your solution is problematic in three aspects: Adding JSON serialization (a presentation layer concern) to an entity object, passing through the entity object to the controller and the use of dynamic. Create a model object from the entity object and extend it with a title property. During the conversion to a model object you can handle the specifics of the dynamic object creation. I'm sure your domain problem doesn't necessary call for a solution that involves dynamic. Commented Jun 11, 2018 at 13:06
  • I have created a form builder so the model must be constructed at run-time according to the fields and values of the form. Commented Jun 11, 2018 at 13:30

1 Answer 1

1

You should be able to just set the value of the Content property as that is the value the DynamicContent property retrieves when you call the get method.

So instead of:

DynamicContent.title = "myvalue"

You would call:

Content = *the json representation of the content*

This would however need to be in JSON format as the DynamicContent getter DeSerializes it from JSON.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.