JSON.NET deserializes it fine, but whatever mvc uses for controller parameter binding barfs hard. Can I do anything else to make this work?
The bits:
public partial class Question
{
public Dictionary<string, List<QuestionExtendedProp>> TemporaryExtendedProperties { get; set; }
}
And the controller method
[HttpPost]
public JsonResult SaveQuestions(Question[] questions)
{
var z =
JsonConvert.DeserializeObject(
"{'Options':[{'PropKey':'asdfasd','PropVal':'asdfalkj'},{'PropKey':'fdsafdsafasdfas','PropVal':'fdsafdas'}]}",
typeof (Dictionary<string, List<QuestionExtendedProp>>)) as Dictionary<string, List<QuestionExtendedProp>>;
//this deserializes perfectly. z is exactly what I want it to be
//BUT, questions is all wrong. See pic below
//lots of code snipped for clarity, I only care about the incoming questions object
return Utility.Save(questions);
}
Here's what MVC gives me for this exact string (Pulled from fiddler, extras snipped for your reading pleasure)
"TemporaryExtendedProperties":{"Options":
[{"PropKey":"NE","PropVal":"NEBRASKA"},
{"PropKey":"CORN","PropVal":"CHILDREN OF"},
{"PropKey":"COW","PropVal":"MOO"}]}
Why does MVC mangle the binding from this perfectly fine json string and how can I get it to not do so? I have complete control over the json structure and creation.
Edit
I tried changing the type of Question.TemporaryExtendedProperties to List<KeyValuePair<string, List<QuestionExtendedProp>>>
, but that didn't work either. Here's the new json (which matches exactly what System.Web.Script.Serialization.JavaScriptSerializer
serializes an object to!)
{
TemporaryExtendedProperties: [
{
Key: 'Options',
Value: [
{
PropKey: 'NEBRASKA',
PropVal: 'NE'
},
{
PropKey: 'DOG',
PropVal: 'CORN'
},
{
PropKey: 'MEOW???',
PropVal: 'COW'
}
]
}
]
}
That didn't work either. It's deserialized by the controller to a List<blah,blah>
properly, with a count of 1 (as expected), but the Key
and Value
are both null
. Json.NET again handles it perfectly.
Ugh.