I'm calling an MVC controller method and passing it an Entity
object. Among other properties, Entity
also has a Contacts
property. With this approach, the controller gets the entity, and the right number of contacts within the entities, but all of the contacts' properties are null.
This is the original approach:
$.post('/Home/Save', $.param(entity), SaveComplete);
With the strongly-typed controller:
public ActionResult Save(Entity entity)
This causes each Entity.Contact
to have null properties:
And Fiddler shows that the contacts are passed to the server.
To get the controller to recognize the Contacts property of Entity, I have to do this:
JavaScript:
$.post('/Home/Save', { entityAsJson: JSON.stringify(entity) }, SaveComplete);
Controller method:
public ActionResult Save(string entityAsJson)
{
try
{
Entity entity = JsonConvert.DeserializeObject<Entity>(entityAsJson);
// more code here
}
}
That's unfortunate, because now my controller takes a string instead of a strongly-typed entity. Is there a way to get the first option to work, or do I need to stringify the JSON?