从表MVC 4编辑/删除数组行

时间:2016-07-20 09:54:36

标签: asp.net asp.net-mvc asp.net-mvc-4

所以我有教室一系列注册,如下所示。

课堂模型:

public class Classroom
    {
        public virtual int ClassroomId { get; set; }
        public virtual string Name { get; set; }
        public virtual List<Enrollment> EnrollmentsClassroom { get; set; }
        public Turma()
        {
            EnrollmentsClassroom = new List<Enrollment>();
        }
    }

报名模式:

public class Enrollment
    {
        public virtual int EnrollmentId { get; set; }
        public virtual int ClassroomId { get; set; }

        public virtual string Name{ get; set; }
        public virtual Classroom ClassroomEnrollment { get; set; }
    }

我制作了一张表格,其中显示了来自特定教室的所有注册

查看:

  @model X.Models.Classroom

            @{
                ViewBag.Title = "Planificacao";
            }

            <h2>Planificacao</h2>

        //this table only shows whats in the array of enrollments from the classroom choosen by the user
            <table>
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                @foreach (var item in Model.EnrollmentsClassroom) //Name of the array of enrollments in model classroom
                {
                    <tr>
                        <td>
                            @Html.DisplayFor(modelItem => item.Name)
                        </td>
                        <td>
//obviously these ActionLinks are wrong:
    @Html.ActionLink("Delete", "Delete", "Enrollment", new { id = Model.ClassroomId }, null))
    @Html.ActionLink("Edit", "Edit", "Enrollment", new { id = Model.ClassroomId }, null))
                        </td>
                    </tr>
                }

            </table> 

我的问题是如何编辑和删除表中的一行,因为视图使用的是“课堂”模型,但该行来自“注册”,因为我无法以这种方式获取注册ID。 我已经看过这篇文章(Deleting row in table with Jquery in mvc project),但它对我没用。 感谢。

1 个答案:

答案 0 :(得分:0)

由于您的foreach中的itemEnrollment,因此您可以使用它来访问注册数据:

@foreach (Enrollment item in Model.EnrollmentsClassroom) {
     /* snip */
     @Html.ActionLink("Delete", "Delete", "Enrollment", new { id = item.EnrollmentId }, null))
     @Html.ActionLink("Edit", "Edit", "Enrollment", new { id = item.EnrollmentId }, null))
     /* snip */
}