I'm trying to add a field to an existing datacontract in our system. (EF code first, POCO's with attribute hell :P)
However, when serializing the contract to the razor view (to stuff into an Angular.js model) the serializer omits the new field. If I step through in debug mode, the Model
object in the view contains the field, but after I use JsonConverter.SerializeObject(Model)
the output model does not include the new field.
I have recycled the apppool, restarted the website and restarted IIS without resolution. I have also inspected the data flow with fiddler to avoid any caching issues on the browser side.
The following workaround does actually work, so the property does exist on the model:
var model = @Html.ToJson(Model);
model.NewProperty = @Model.NewProperty;
return {
model: model
};
...where Html.ToJson(Model)
is an extension method that just calls JsonConvert.SerializeObject(Model)
and stuffs it into a MvcHtmlString
.
Does anyone know what is going on? According to this answer there is some form of type information caching in json.net but it's hard to find any more information about it.