Basically, JavaScriptSerializer
doesn't support that pattern of conditional serialization. So: use one that does - fortunately, Json.NET does support this, and works fine:
string jsonData = JsonConvert.SerializeObject(pageModelInstance);
(no other changes needed, although you can safely remove the [Serializable]
- that isn't needed)