我正在关注Getting Started with Entity Framework 6 Code First using MVC 5教程。在本教程中,在视图中有两个表。当您从第一个(讲师)表中选择一行时,下表显示该讲师的课程。我无法理解这是怎么回事。在代码中哪些地方知道教师的课程?如果有人能向我解释这一点,我会非常高兴。提前致谢。 这是控制器:
public ActionResult Index(int? id, int? courseID)
{
var viewModel = new InstructorIndexData();
viewModel.Instructors = db.Instructors.Include(i => i.OfficeAssignment).Include(i => i.Courses.Select(c => c.Department))
.OrderBy(i => i.LastName);
if(id != null)
{
ViewBag.InstructorID = id.Value;
viewModel.Courses = viewModel.Instructors.Where(i => i.ID == id.Value).Single().Courses;
}
if(courseID != null)
{
ViewBag.CourseID = courseID.Value;
viewModel.Enrollments = viewModel.Courses.Where(x => x.CourseID == courseID).Single().Enrollments;
}
return View(viewModel);
}
以下是观点:
@model ContosoUniversity.ViewModels.InstructorIndexData
@{
ViewBag.Title = "Instructors";
}
<h2>Instructors</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
Last Name
</th>
<th>
First Name
</th>
<th>
Hire Date
</th>
<th>
Office
</th>
<th></th>
</tr>
@foreach (var item in Model.Instructors)
{
string selectedRow = "";
if (item.ID == ViewBag.InstructorID)
{
selectedRow = "success";
}
<tr class="@selectedRow">
<td>
@Html.DisplayFor(modelItem => item.LastName)
</td>
<td>
@Html.DisplayFor(modelItem => item.FirstMidName)
</td>
<td>
@Html.DisplayFor(modelItem => item.HireDate)
</td>
<td>
@if (item.OfficeAssignment != null)
{
@item.OfficeAssignment.Location
}
</td>
<td>
@Html.ActionLink("Select", "Index", new { id = item.ID }) |
@Html.ActionLink("Edit", "Edit", new { id = item.ID }) |
@Html.ActionLink("Details", "Details", new { id = item.ID }) |
@Html.ActionLink("Delete", "Delete", new { id = item.ID })
</td>
</tr>
}
</table>
@if (Model.Courses != null)
{
<h3>Courses Taught by Selected Instructor</h3>
<table class="table">
<tr>
<th></th>
<th>Number</th>
<th>Title</th>
<th>Department</th>
</tr>
@foreach (var item in Model.Courses)
{
string selectedRow = "";
if (item.CourseID == ViewBag.CourseID)
{
selectedRow = "success";
}
<tr class="@selectedRow">
<td>
@Html.ActionLink("Select", "Index", new { courseID = item.CourseID })
</td>
<td>
@item.CourseID
</td>
<td>
@item.Title
</td>
<td>
@item.Department.Name
</td>
</tr>
}
</table>
}
答案 0 :(得分:2)
if(id != null)
{
ViewBag.InstructorID = id.Value;
viewModel.Courses = viewModel.Instructors.Where(i => i.ID == id.Value).Single().Courses;
}
这是根据教师选择课程的代码。注意它首先根据id参数选择讲师并读取该讲师的课程
答案 1 :(得分:1)
我认为代码很清楚, 在索引操作中,它找到具有给定id的教师, 这个街区:
def my_view(request)
data = data['my_data']
if request.method == 'POST':
data_a = request.POST.get(data["data_a"])
data_b = request.POST.get(data["data_b"])
data_c = request.POST.get(data["data_c"])
data_d = request.POST.get(data["data_d"])
obj = WeatherData.objects.create(
data_a=data_a, data_b=data_b,
data_c=data_c, data_d=data_d,)
# obj.save() --> you don't need save because you already called `create`
return render(request, 'data.html', obj)
一切都由ViewBags完成,ViewBag.InstructorID定义了所选的
'讲师',查看页面中的此块
if(id != null)
{
ViewBag.InstructorID = id.Value;
viewModel.Courses = viewModel.Instructors.Where(i => i.ID == id.Value).Single().Courses;
}
选择具有“成功”类的“tr标签”
相同的课程逻辑