如何在View MVC3 ASP.NET中查看多个表

时间:2012-05-04 06:28:49

标签: asp.net-mvc-3 razor

我试图在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]'. 

3 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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>

答案 2 :(得分: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 }

希望能让你走上正轨。