【发布时间】:2014-10-16 10:54:41
【问题描述】:
我已经关注 JSON:
var QuestionTemplate = {
Name: "Simple addition",
MathML: "mathML goes here",
Expression: "a+b",
QType: "mcq",
Rules: {
a: {//a is variable from expression(a+b)
variableType: "Single Digit",
min: "1",
max: "6"
},
b: {//b is variable from expression(a+b)
variableType: "Two Digit",
min: "20",
max: "80"
}
}
};
在规则中,“a”和“b”是来自表达式“a+b”的变量。如果表达式为a+b+c,则有a、b、c三个变量。
现在我想将上面的 json 映射到我在 MVC 中的模型。
到目前为止我已经尝试过:
型号:
public class QuestionTemplateModel
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
public string Name { get; set; }
public string MathML { get; set; }
public string Expression { get; set; }
public string QType { get; set; }
public Rules Rules { get; set; }
}
public class Rules
{
public List<variable> variable { get; set; }
}
public class variable
{
public List<VariableDetails> VariableDetails { get; set; }
}
public class VariableDetails
{
public string variableType { get; set; }
public string min { get; set; }
public string max { get; set; }
}
但是当我将我的 json 发送到模型时,Rules 为 null,其中 Name、MathML、Expression、QType 被正确映射。 我需要如何构建我的模型。我做错了什么。
【问题讨论】:
-
在您的 JSON 规则属性中是一个集合类型,而在您的 QuestionTemplate 模型中,它是一个对象
-
您真的很确定在之前就 SO 上的同一主题(模型绑定)给出的众多答案中,没有一个提供解决您的问题所需的信息吗?
标签: c# asp.net-mvc json asp.net-mvc-4 model