【问题标题】:Compound primary key edit view MVC复合主键编辑视图 MVC
【发布时间】:2014-11-16 05:13:53
【问题描述】:

您好,我正在尝试在包含学生 ID 和课程 ID 的链接器表中编辑学生成绩。我环顾四周,发现我需要将两个 id 参数放入视图中,而不仅仅是一个 id,然后将操作链接更改为采用两个参数。但是当我点击索引中的编辑按钮时,我得到一个 404 说找不到页面。有什么想法我哪里出错了吗? 提前致谢。

这是我的索引视图

@model IEnumerable<S00132671CA2.Models.StudentCourse>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Grade)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Grade)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id= item.StudentId, CourseId = item.CourseId }) |
            @Html.ActionLink("Details", "Details", new { id = item.StudentId, Course = item.CourseId }) |
            @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
        </td>
    </tr>
}

</table>

这是我的编辑操作

 public ActionResult Edit(int? StudentId, int? CourseId)
        {
            StudentCourse courseList = db.StudentCourse.Find(StudentId, CourseId);
            if (courseList == null)
            {
                return HttpNotFound();
            }
            ViewBag.StudentId = new SelectList(db.Students, "StudentId", "StudentName", courseList.StudentId);
            ViewBag.CourseId = new SelectList(db.Courses, "CourseId", "CourseName", courseList.CourseId);
            return View(courseList);
        }

如果有帮助,这是我的模型

public class StudentCourse
    {
         [Key, Column(Order = 0)]
        public int StudentId { get; set; }
         public virtual Student Student { get; set; }
          [Key, Column(Order = 1)]
        public int CourseId { get; set; }
          public virtual Course Course { get; set; }
          public double Grade { get; set; }

    }

【问题讨论】:

    标签: asp.net-mvc


    【解决方案1】:

    试试

    @Html.ActionLink("Edit", "Edit", new { StudentId = item.StudentId, CourseId = item.CourseId })
    

    【讨论】:

    • 就是这样!感谢您抽出时间来回答!
    猜你喜欢
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 2022-01-08
    • 1970-01-01
    • 1970-01-01
    • 2018-06-01
    • 1970-01-01
    相关资源
    最近更新 更多