无法在Fiddler中显示第二个表的内容

时间:2015-12-02 11:23:06

标签: c# asp.net sql-server asp.net-web-api2 fiddler

我正在使用Linq加入2个表。我能够获取第一个表的内容,但将第二个表作为null。如何将第二个表的内容也提取到单个JSON对象中。我的代码如下:

    public static IEnumerable<Tbl_Students> GetAllStudents()
    {
        StudentDBEntities dataContext = new StudentDBEntities();

        var query = (from student in dataContext.Tbl_Students
                     join subject in dataContext.Tbl_Subjects on student.Roll_Number equals subject.Roll_Number
                     select new 
                     {
                         Roll_Number = student.Roll_Number,
                         FirstName = student.FirstName,
                         LastName = student.LastName,
                         Class = student.Class,
                         Gender = student.Gender,
                         Science = subject.Science,
                         Social = subject.Social,
                         Mathematics = subject.Mathematics,
                         Total = subject.Total
                     }).ToList().Select(s => new Tbl_Students
                     {
                         Roll_Number = s.Roll_Number,
                         FirstName = s.FirstName,
                         LastName = s.LastName,
                         Class = s.Class,
                         Gender = s.Gender
                     });



        return query;
    }

两个表结构是:

学生

public class Student
{
    public int Roll_Number { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Class { get; set; }
    public string Gender { get; set; }
}

主题

class Subject
{
    public int Roll_Number { get; set; }
    public int Science { get; set; }
    public int Social { get; set; }
    public int Mathematics { get; set; }
    public int Total { get; set; }


}

除了Fiddler的主题外,我得到了所有东西。

添加图片

enter image description here

1 个答案:

答案 0 :(得分:1)

问题是你永远不会从查询中填充Tbl_Subjects。我已更新您的查询以填充Tbl_Subjects。

var oReq = new XMLHttpRequest(); //New request object
oReq.onload = function() {
    var Json = JSON.parse(this.responseText);
    for (value in Json.prods[0]){
        alert(value);
    }
    var newTable = document.createElement('table');

    //Create first row
    var tableRowFirst = document.createElement('tr');

    for (key in Json.prods[0]) {
        //create new heading
        var keys = document.createElement('th');

        // append Heading to table
        tableRowFirst.appendChild(keys);

        //set new heading text content to json information
        keys.textContent = key;
    };

    //Create rows
    var tableBody = document.createElement('tbody');
    var tableRow = document.createElement('tr');
    tableBody.appendChild(tableRow);

    for (key in Json.prods[0]) {
        //create new cell
        var values = document.createElement('td');

        // append cell to row
        tableRow.appendChild(values);

        //set new content text content to json information
        values.textContent = Json.prods[0].key;
    };

    //Append table to DOM
    document.body.appendChild(newTable);

    //Append rows to new table
    newTable.appendChild(tableRowFirst);
    newTable.appendChild(tableBody);
};
oReq.open("get", "../php/getalltag.php", true);
oReq.send();