【发布时间】:2012-11-13 18:22:45
【问题描述】:
我有一个强类型视图,它显示数据库表“Student_A”中的学生
查看:
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<IEnumerable<Student.Models.Student_A>>" %>
<table>
<% foreach (var item in Model) { %>
<tr>
<td>
<%= Html.Encode(item.StudentName) %>
</td>
<td>
<%= Html.Encode(item.StudentId) %>
</td>
</tr>
<% } %>
</table>
控制器:
public ActionResult ShowStudents()
{
ViewData.Model = stud.Student_A.Where(a => a.StudentId != 0);
return View();
}
我有另一个表“Student_B”也存储学生,我也想在我的视图中显示这个学生。比如:
public ActionResult ShowAllStudents()
{
var StudentA = stud.Student_A.Where(a => a.StudentId != 0);
var StudentB = stud.Student_B.Where(a => a.StudentId != 0);
ViewData.Model = StudentA + StudentB;
return View();
}
是否可以在单个控制器操作中在强类型视图中显示来自两个不同表的数据?还是我必须创建一个数据库视图来显示“Student_A”和“Student_B”表中的学生,然后在视图中显示它们?
非常感谢任何帮助
【问题讨论】:
标签: c# asp.net-mvc linq asp.net-mvc-4 razor