实体框架外键连接

时间:2014-04-11 08:12:34

标签: asp.net-mvc-4 frameworks entity

这是我的控制器:

  public ActionResult Display(int id)
    {
        Child child = db.Children.SingleOrDefault(c => c.ChildID == id);
        ViewBag.ChildName = child.FamilyName + ", " + child.GivenName + ", " + child.MiddleName;

        EducationalHistory eduHist = new EducationalHistory();
        eduHist.ChildID = id;

        child.EducationalHistories.Add(eduHist);

        return PartialView(child);
    }

我实际上尝试将内部连接到多个表中,我已经检查了查询,这似乎是正确的代码:

  var chs = from eh in db.EducationalHistories
                  from shl in db.SchoolLevels
                  from shy in db.SchoolYears
                  from ar in db.Areas
                  from sh in db.Schools
                  from gr in db.GradeLevels
                  where eh.SchoolLevelID == shl.SchoolLevelID 
                  where eh.SchoolYearID == shy.SchoolYearID
                  where eh.AreaID == ar.AreaID
                  where eh.SchoolID == sh.SchoolID
                  where eh.GradeLevelID== gr.GradeLevelID
                  select new
                  {
                      eh.ChildID,
                      shl.SchoolLevelName,
                      shy.SchoolYearName,
                      ar.AreaName,
                      sh.SchoolName,
                      gr.GradeLevelName
                  };


        ViewBag.EducationalList = chs;         
        EducationalHistory eduHist = new EducationalHistory();
        eduHist.ChildID = id;
       // child.EducationalHistories.Add(eduHist);

      // ViewBag.SelectList = db.EducationalHistories.Find(id);

        return PartialView(chs);

但我现在的问题是我不知道如何将数据从CHS变量传输到局部视图。 我主要使用模型类在视图模型上过去模型值。我可以使用viewbag传输查询数据吗?

再次感谢你。

0 个答案:

没有答案