I have a complex view model structure. When I try to send this view model to as a ApiController return value and let WebApi use the JsonMediaTypeFormatter, I get an InvalidProgramException: "Error getting value from 'CS$<>9__CachedAnonymousMethodDelegate2' on 'blabla.ViewModels.AnswerFilterModel'."
Exception callstack:
bei Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(Object target)
bei Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues(JsonWriter writer, Object value, JsonContainerContract contract, JsonProperty member, JsonProperty property, JsonContract& memberContract, Object& memberValue)
bei Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
bei Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
bei Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value)
bei Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value)
bei Newtonsoft.Json.JsonSerializer.Serialize(JsonWriter jsonWriter, Object value)
bei System.Net.Http.Formatting.JsonMediaTypeFormatter.<>c__DisplayClassd.b__c()
bei System.Threading.Tasks.TaskHelpers.RunSynchronously(Action action, CancellationToken token)
The model that causes this exception looks like this:
[Serializable]
public class AnswerFilterModel
{
[NonSerialized]
private readonly List answerValues;
/// <summary>
/// Initializes a new instance of the class.
/// </summary>
public AnswerFilterModel()
{
this.answerValues = (from answer in Enum.GetNames(typeof(Antworten))
select new SelectListItem { Text = answer, Value = answer, Selected = false })
.ToList();
}
/// <summary>
/// Gets or sets a value indicating whether active.
/// </summary>
public bool Active { get; set; }
/// <summary>
/// Gets or sets a value indicating whether ja.
/// nach bisherigen Antworten.
/// </summary>
public bool Ja { get; set; }
/// <summary>
/// Gets or sets a value indicating whether handlungsbedarf.
/// </summary>
public bool Handlungsbedarf { get; set; }
/// <summary>
/// Gets or sets a value indicating whether beratungsbedarf.
/// </summary>
public bool Beratungsbedarf { get; set; }
/// <summary>
/// Gets or sets a value indicating whether unzutreffend.
/// </summary>
public bool Unzutreffend { get; set; }
/// <summary>
/// Gets or sets a value indicating whether unbeantwortet.
/// </summary>
public bool Unbeantwortet { get; set; }
/// <summary>
/// Gets the answer values.
/// </summary>
public IEnumerable AnswerValues
{
get { return this.answerValues; }
}
}
The enum Antworten contains 6 values, numbered from 0 to 5. The List and IEnumerable are generic, the codeplex text editor eats the generics. It's "List of Antworten" and "IEnumerable of Antworten".
Comments: ** Comment from web user: CarstenSchuette **
Exception callstack:
bei Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(Object target)
bei Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues(JsonWriter writer, Object value, JsonContainerContract contract, JsonProperty member, JsonProperty property, JsonContract& memberContract, Object& memberValue)
bei Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
bei Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
bei Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value)
bei Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value)
bei Newtonsoft.Json.JsonSerializer.Serialize(JsonWriter jsonWriter, Object value)
bei System.Net.Http.Formatting.JsonMediaTypeFormatter.<>c__DisplayClassd.b__c()
bei System.Threading.Tasks.TaskHelpers.RunSynchronously(Action action, CancellationToken token)
The model that causes this exception looks like this:
[Serializable]
public class AnswerFilterModel
{
[NonSerialized]
private readonly List answerValues;
/// <summary>
/// Initializes a new instance of the class.
/// </summary>
public AnswerFilterModel()
{
this.answerValues = (from answer in Enum.GetNames(typeof(Antworten))
select new SelectListItem { Text = answer, Value = answer, Selected = false })
.ToList();
}
/// <summary>
/// Gets or sets a value indicating whether active.
/// </summary>
public bool Active { get; set; }
/// <summary>
/// Gets or sets a value indicating whether ja.
/// nach bisherigen Antworten.
/// </summary>
public bool Ja { get; set; }
/// <summary>
/// Gets or sets a value indicating whether handlungsbedarf.
/// </summary>
public bool Handlungsbedarf { get; set; }
/// <summary>
/// Gets or sets a value indicating whether beratungsbedarf.
/// </summary>
public bool Beratungsbedarf { get; set; }
/// <summary>
/// Gets or sets a value indicating whether unzutreffend.
/// </summary>
public bool Unzutreffend { get; set; }
/// <summary>
/// Gets or sets a value indicating whether unbeantwortet.
/// </summary>
public bool Unbeantwortet { get; set; }
/// <summary>
/// Gets the answer values.
/// </summary>
public IEnumerable AnswerValues
{
get { return this.answerValues; }
}
}
The enum Antworten contains 6 values, numbered from 0 to 5. The List and IEnumerable are generic, the codeplex text editor eats the generics. It's "List of Antworten" and "IEnumerable of Antworten".
Comments: ** Comment from web user: CarstenSchuette **
Hi James, unfortunately, removing the [Serializable] attribute was a workaround does not help.