我一直在关注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”的变量。如果expression是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正确映射。我需要如何构造我的模型。我在做什么错。
我认为您不需要那么多的嵌套对象-字典应该可以工作... [编辑]您也需要创建字典。字典键将是您的变量名(a,b)。请尝试以下操作:
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 Dictionary<string, VariableDetails> Rules = new Dictionary<string, VariableDetails>() { get; set; }
}
public class VariableDetails
{
public string variableType { get; set; }
public string min { get; set; }
public string max { get; set; }
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句