MVC创建、编辑、删除操作”;无法隐式转换类型“”;错误

本文关键字:转换 类型 错误 操作 删除 创建 MVC 编辑 | 更新日期: 2023-09-27 18:25:26

我要为我的表创建一个删除按钮

这是我的代码

<center>
    <H2>LIST OF REGISTERED STUDENTS</H2>
    <br /><br />
</center>
<table class="table">
    <tr>
        <th>
            Name
        </th>
        <th>
           Last Name
        </th>
        <th>
            &nbsp;&nbsp;E-Mail
        </th>
        <th>
            Password
        </th>
        <th>
            Student Number
        </th>
        <th>
            &nbsp;Program
        </th>
        <th></th>
    </tr>
@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.student_name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.student_lname)
        </td>
        <td>
             &nbsp;&nbsp;@Html.DisplayFor(modelItem => item.student_email)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.student_password)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.student_number)
        </td>
        <td>
            &nbsp;@Html.DisplayFor(modelItem => item.student_program)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.student_name }) |
            @Html.ActionLink("Details", "Details", new { id=item.student_name }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.student_name })
        </td>
    </tr>
}
</table>

但是当我创建删除语法时

 public ActionResult Delete(int id = 0)
        {
            CSdbConnectionString db = new CSdbConnectionString();
            student student = db.students.Find(id);
            if(student == null)
            {
                return HttpNotFound();
            }
            return View(student);
        }
        // POST: Student/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            CSdbConnectionString db = new CSdbConnectionString();
            try
            {
                student student = db.students.Find(id);
                db.students.Remove(student);
                db.SaveChanges();
                return RedirectToAction("ViewStudents","ConsulSked");
            }
            catch
            {
                return View();
            }
        }

代码CCD_ 1有错误

无法将类型CS.Models.student隐式转换为CS.student

这是我的学生班

  [Table("student")]
    public class student
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int student_id { get; set; }
        public string student_name { get; set; }
        public string student_lname { get; set; }
        public string student_email { get; set; }
        public string student_password { get; set; }
        public string student_number { get; set; }
        public string student_program { get; set; }
    }

这是我的数据上下文类

   public class CSdbConnectionString : DbContext
    {
        public CSdbConnectionString()
        : base("CSdbConnectionString")
        { }
        public DbSet<appointment> appointments { get; set; }
        public DbSet<faculty> faculties { get; set; }
        public DbSet<sched> scheds { get; set; }
        public DbSet<student> students { get; set; }
}

我该怎么办?我无法创建删除选项。

MVC创建、编辑、删除操作”;无法隐式转换类型“”;错误

我已经找到了解决方案,适用于将来出现同样错误的人。

这是因为我的项目中有一个.dbml文件,所以我可以存储我的存储过程。这就是

CS研究

我的名称空间模型中的一个是

CS模型研究

而这正是我们需要的。

所以不是

student student = db.students.Find(id);

使其成为

CS.Models.student student = db.students.Find(id);