1

我正在尝试使用 MVC3 Razor 中的 LINQ 从数据库中选择多个表。为了在视图中查看多个表中的多个数据,我需要对我的代码进行哪些更改?

这是我的模特

public class DailyAttendance
{
    [Key]
    public string pnp { get; set; }
    public string student_id { get; set; }
    public string mth_yr_id { get; set; }
    public string D01 { get; set; }
    public string D02 { get; set; }
} // and so on

public class TitasDb : DbContext
{
    public DbSet<DailyAttendance> DailyAttendance { get; set; }
    public DbSet<MonthYear> MonthYear { get; set; }
    // and so on

}


这就是我在控制器中使用 LINQ 的方式

public class AttendanceController : Controller
{
    TitasDb _attendanceModel = new TitasDb();

    public ActionResult Index()
    {
        var model = from s in _attendanceModel.Student
                    join ssc in _attendanceModel.StudentSchoolClass on s.student_id equals ssc.student_id
                    join sc in _attendanceModel.SchoolClass on ssc.school_class_id equals sc.school_class_id
                    join da in _attendanceModel.DailyAttendance on s.student_id equals da.student_id
                    join my in _attendanceModel.MonthYear on da.mth_yr_id equals my.mth_yr_id
                    where my.month_id == "M05"
                    where sc.teacher_id == "T1000001"
                    select new { DailyAttendance = da, Student = s };


        return View(model);
    }
}


这就是我尝试查看数据的方式

@model IEnumerable<titas.Models.DailyAttendance>

@foreach (var item in Model) {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.student_id)
    <td>
</tr>
}


我得到的错误是

The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery`1[<>f__AnonymousType5`2[titas.Models.DailyAttendance,titas.Models.Student]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[titas.Models.DailyAttendance]'. 
4

3 回答 3

7

与每个 ASP.NET MVC 应用程序一样,定义一个视图模型,该模型将包含您需要在视图中使用的所有信息:

public class MyViewModel
{
    public DailyAttendance DailyAttendance { get; set; }
    public Student Student { get; set; }
}

然后让您的控制器操作将此视图模型返回到视图:

public class AttendanceController : Controller
{
    TitasDb _attendanceModel = new TitasDb();

    public ActionResult Index()
    {
        var model = from s in _attendanceModel.Student
                    join ssc in _attendanceModel.StudentSchoolClass on s.student_id equals ssc.student_id
                    join sc in _attendanceModel.SchoolClass on ssc.school_class_id equals sc.school_class_id
                    join da in _attendanceModel.DailyAttendance on s.student_id equals da.student_id
                    join my in _attendanceModel.MonthYear on da.mth_yr_id equals my.mth_yr_id
                    where my.month_id == "M05"
                    where sc.teacher_id == "T1000001"
                    select new MyViewModel
                    { 
                        DailyAttendance = da, 
                        Student = s 
                    };
        return View(model.ToList());
    }
}

最后将您的视图强类型化到视图模型中:

@model IList<MyViewModel>
<table>
    <thead>
        <tr>
            <th>student id</th>
            <th>attendance id</th>
        </tr>
    </thead>
    <tbody>
        @for (var i = 0; i < Model.Count; i++)
        {
            <tr>
                <td>@Html.DisplayFor(x => x[i].Student.student_id)</td>
                <td>@Html.DisplayFor(x => x[i].DailyAttendance.attendance_id)</td>
            </tr>
        }
    </tbody>
</table>

为了清理视图,您可以使用一个显示模板,该模板将为模型的每个元素自动呈现 ( ~/Views/Shared/DisplayTemplates/MyViewModel.cshtml):

@model MyViewModel
<tr>
    <td>@Html.DisplayFor(x => x.Student.student_id)</td>
    <td>@Html.DisplayFor(x => x.DailyAttendance.attendance_id)</td>
</tr>

现在你的主要观点就变成了:

@model IList<MyViewModel>
<table>
    <thead>
        <tr>
            <th>student id</th>
            <th>attendance id</th>
        </tr>
    </thead>
    <tbody>
        @Html.DisplayForModel()
    </tbody>
</table>
于 2012-05-04T06:37:37.887 回答
1

您正在向您的视图发送匿名对象并声明您的视图IEnumerable<titas.Models.DailyAttendance>在这种情况下不正确。尝试为它制作模型。

public class MyModel
{
   public DailyAttendance dailyAttendance{ get; set; }
   public Student student { get; set; }
}

然后在您的控制器中将数据填充到此模型中

    var model = (from s in _attendanceModel.Student
                join ssc in _attendanceModel.StudentSchoolClass on s.student_id equals ssc.student_id
                join sc in _attendanceModel.SchoolClass on ssc.school_class_id equals sc.school_class_id
                join da in _attendanceModel.DailyAttendance on s.student_id equals da.student_id
                join my in _attendanceModel.MonthYear on da.mth_yr_id equals my.mth_yr_id
                where my.month_id == "M05"
                where sc.teacher_id == "T1000001"
                select new MyModel { dailyAttendance = da, student = s }).ToList();

然后将它传递给您的视图并像这样声明您的视图模型

@model IEnumarable<MyModel>
于 2012-05-04T06:37:58.613 回答
0

问题是您将匿名类型作为模型传递:select new { DailyAttendance = da, Student = s }您在视图中将模型定义为@model IEnumerable<titas.Models.DailyAttendance>.

尝试创建一个特定于您的视图的模型:

public class AttendanceModel
{
    public Student Student { get; set; }
    public DailyAttendance DailyAttendace { get; set; }
}

或者任何对你有用的东西。您可能还想为学生和日常出勤创建特定的视图模型。

然后你可以改变你的模型:

@model IEnumerable<Attendanceodel>

并选择:

select new AttendanceModel { DailyAttendance = da, Student = s }

希望这能让你走上正轨。

于 2012-05-04T06:39:16.357 回答