0

I've following 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"
        }
    }
};

In rules "a" and "b" are variables from expression "a+b". If expression is a+b+c, there will be three variables a,b and c.

Now I want to map above json to my model in MVC.

What I've tried so far:

Model:

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; }
}

But when I send my json to model Rules is null, where as Name, MathML, Expression, QType are mapped properly. How I need to structure my model. What mistake I am doing.

2
  • In your JSON rules property is a collection type, while in your QuestionTemplate model, its an single object Commented Oct 16, 2014 at 11:02
  • Are you really quite sure that out of the many answers given on the same topic (model binding) on SO before, not one provides the information needed to solve your problem as well? Commented Oct 16, 2014 at 11:17

2 Answers 2

2

I don't think you need so many nested objects - a dictionary should work... [EDIT] you need to create the dictionary, too. The dictionary keys will be your variable names (a, b).Try this:

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; }
}
Sign up to request clarification or add additional context in comments.

Comments

0

In your bellow code

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: and b: will not match with any property so it won't bind that Rules Property, what you can do is remove that a: and b: name and format your JSON in following way

{
        variableType: "Single Digit",
        min: "1",
        max: "6"
    },
    {
        variableType: "Two Digit",
        min: "20",
        max: "80"
    }

1 Comment

Above ans by @mlinth would be a better option, it should work, if JSON format is in your control then try above code in my ans.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.