简体   繁体   English

在ASP.NET MVC 5中的表中选择一行

[英]Selecting a row in a table in ASP.NET MVC 5

I am following Getting Started with Entity Framework 6 Code First using MVC 5 tutorial. 我正在使用MVC 5教程学习Entity Framework 6 Code First入门 In this tutorial, in a view there are two tables. 在本教程中,一个视图中有两个表。 When you select a row from first (Instructor) table, the below table displays that Instructor's courses. 当从第一张(教师)表格中选择一行时,下表显示该教师的课程。 I can not understand how is this happening. 我不明白这是怎么回事。 Where in the code does it know that instructor's courses? 它在代码中哪里知道该讲师的课程? If someone could explain this to me, I'd be very glad. 如果有人可以向我解释这一点,我将非常高兴。 Thanks in advance. 提前致谢。 Here is the controller : 这是控制器:

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);
        }

Here is the view : 这是视图:

@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>
}
if(id != null)
{
    ViewBag.InstructorID = id.Value;
    viewModel.Courses = viewModel.Instructors.Where(i => i.ID == id.Value).Single().Courses;
}

This is the code that selects the courses based on the instructor. 这是根据讲师选择课程的代码。 Pay attention to the fact that it first selects the Instructor based on the id parameter and the reads the Courses of that Instructor 注意以下事实:它首先根据id参数选择讲师,然后读取该讲师的课程

i think the code is clear, in index action it finds the Instructor with given id, this block: 我认为代码很清楚,在索引操作中它找到具有给定id的Instructor,此块:

if(id != null)
{
 ViewBag.InstructorID = id.Value;
viewModel.Courses = viewModel.Instructors.Where(i => i.ID == id.Value).Single().Courses;
}

everything is done by ViewBags , ViewBag.InstructorID defines selected 一切都由ViewBags完成,ViewBag.InstructorID定义了selected

'Instructor' , this block in View page “讲师”,“查看”页面中的此块

if (item.ID == ViewBag.InstructorID)
{
 selectedRow = "success";
}

'tr tag' that has "success" class is selected 选择具有“成功”类别的“ tr标签”

same logic for courses 课程的逻辑相同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM