jonny
jonny

Reputation: 797

Get one to may relationship data from tables as JSON

enter image description here

I have this two tables from the diagrams.For every question i can have multiple answer. I want to get data by question id in json format like this:

var initialData = [
    {question_id: "1", Description: "Danny", Status: "1", answers: [
        { answer_id: "1", description: "AAAAA" },
        { answer_id: "2", description: "Bbbbb" }]
    }
];

I use this code

  var question = db.Questions.FirstOrDefault((p) => p.questionID== id);
  return this.Json(question , JsonRequestBehavior.AllowGet);

and i get this

var initialData = [
        {question_id: "1", Description: "Danny", Status: "1", answers:null

        }
    ];

Can you help my please with a solution.

Upvotes: 1

Views: 417

Answers (1)

Swaraj
Swaraj

Reputation: 383

Can you try this please.

var question = db.Questions.Include("Answer").FirstOrDefault((p) => p.questionID== id);

Have a look at the ado.Net blog

Upvotes: 2

Related Questions