C# MVC w/实体框架从dropdownlist重定向到编辑页面

C# MVC w/实体框架从dropdownlist重定向到编辑页面,c#,asp.net,asp.net-mvc,model-view-controller,C#,Asp.net,Asp.net Mvc,Model View Controller,大家好!新手还在这里学习,我现在正在尝试创建一个带有实体框架应用程序(网站?)的小型MVC。我已经到了可以创建、编辑、删除新用户的地步。现在我已经创建了一个新页面,从dropdownlist中我想从数据库中选择一个人名(我在另一个基础上输入的),现在在dropdownlist填充之后,我想能够单击他们的姓名并自动重定向到persons edit页面,或者单击一个将我带到他们页面的编辑按钮 [dropddownlist]

大家好!新手还在这里学习,我现在正在尝试创建一个带有实体框架应用程序(网站?)的小型MVC。我已经到了可以创建、编辑、删除新用户的地步。现在我已经创建了一个新页面,从dropdownlist中我想从数据库中选择一个人名(我在另一个基础上输入的),现在在dropdownlist填充之后,我想能够单击他们的姓名并自动重定向到persons edit页面,或者单击一个将我带到他们页面的编辑按钮

[dropddownlist]<其中包含名称

[John Smith 1]<现在我要点击一个名字,1代表他的学生ID,“John”和“Smith”都是数据库表中的独立部分

public ActionResult Index(string ddl)
{
    ViewBag.ddl = (from r in db.Students select r.FirstN + "," + r.LastN);
}
public ActionResult Edit(string ddl)
{
     Student student = db.Students.Find(ddl);
     if (student== null)
     {
         return HttpNotFound();
     }
     return View(student);
}
[John Smith 1][Edit]<现在我在
下拉列表中选择了他的名字
,我可以单击小编辑按钮,它将带我进入小项目的另一部分编辑页面<代码>本地主机/员工/编辑/1

在我的下拉列表控制器中,我有一个(
FirstN
是名字,
LastN
是数据库中的姓氏)

根据我的观点,我有这个

@Html.DropDownList("Edit", new SelectList(ViewBag.ddl));
@Html.ActionLink("Edit", "Edit", new SelectList(ViewBag.ddl))
这似乎不起作用,我也没什么进展。所以我想尝试一种不同的方法来达到目的

我请求帮助的问题是:我想将他们的姓名设置为
studentID
(这是从数据库中获取的,不能硬编码
FirstN:John LastN=Smith studentID=1)
但然后单击编辑或搜索按钮,从
localhost/editsudent
转到
localhost/student/edit/1
,它将带您进入“John Smith”编辑页面


感谢所有花时间阅读本文的人

好的。。。让我以我所知道的方式向你解释。。也许其他人会有一个更简单的方法

我解释这一点时没有在代码编辑器中尝试,所以如果有任何人注意到任何错误,请通知我

我希望您需要将所选值获取到控制器中的post方法。 把我们的名字叫做学生。因此,要完成我们的工作,我们需要创建一个模型

public class StudentListModel
{
 public List<Student> StudentList{ get; set;}
 public SelectList StudentSelectList { get; set;}
 public int SelectedStudentId { get; set;}
}

[HttpGet]
public ActionResult StudentList()
{
    StudentListModel studentList = new StudentListModel();
    studentList.StudentList = //Code to load the student.
    studentList.StudentSelectList = new SelectList(studentList.StudentList, "StudentId", "StudentName");
    return View(studentList);
}
然后在controller中,post方法如下所示

[HttpPost]
public ActionResult StudentList(StudentListModel studentListModel)
{           
int SelectedStudentValue = studentListModel.SelectedStudentId;
 // Do other operations with the id
 return View();
}
[HttpPost]
public ActionResult StudentList(StudentListModel studentListModel)
{           
int SelectedStudentValue = studentListModel.SelectedStudentId;
 // Do other operations with the id
 return View();
}