[英]MVC w/ Entity Framework redirecting to edit page from dropdownlist
Hello again stackoverflow! 您好,再次stackoverflow! Newbie still learning here, I am now trying to create a small MVC with entity framework application(website?).
新手仍在这里学习,我现在正在尝试使用实体框架应用程序(网站?)创建一个小型MVC。 I've gotten to the point where I can create, a new user, edit, delete them.
我已经可以创建新用户,编辑,删除它们了。 Now I've created a new page and from a dropdownlist I want to select a persons name (that I've entered on the other base) from the database, now after the dropdownlist is populated, I want to be able to click on their name and either automatically redirect to the persons edit page, or click an edit button that takes me to their page.
现在,我创建了一个新页面,并从一个下拉列表中,我想从数据库中选择一个人名(我在另一个基础上输入的人名),现在在填充下拉列表后,我希望能够单击他们的名称,然后自动重定向到人员编辑页面,或单击将我带到其页面的编辑按钮。
[dropddownlist] < This contains the names [dropddownlist] <其中包含名称
[John Smith 1] < Now I would click on a name, the 1 represents his student ID and both "john" and "smith" are separate parts of the table in the database. [John Smith 1] <现在,我要单击一个名称,1代表他的学生ID,“ john”和“ smith”都是数据库中表的独立部分。
[John Smith 1] [Edit] < now that I have his name selected in the dropdownlist
I can click the little edit button and it takes me to another part of my little project the edit page! [John Smith 1] [Edit] <现在在
dropdownlist
选择了他的名字,我可以单击小编辑按钮,然后将我带到小项目的另一部分编辑页面! localhost/employees/edit/1
In my drop down list controller I have this ( FirstN
is First Name and LastN
is Last Name in the database). 在我的下拉列表控制器中,我有这个(
FirstN
是First Name, LastN
是Last Name)。
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);
}
Under the view I have this 根据我的看法
@Html.DropDownList("Edit", new SelectList(ViewBag.ddl));
@Html.ActionLink("Edit", "Edit", new SelectList(ViewBag.ddl))
This doesn't seem to be working and I'm not really getting anywhere. 这似乎不起作用,而且我什么都没有。 So I want to try a different approach on how to get there.
所以我想尝试一种不同的方法去那里。
My question I'm asking help for is: I want to set their names to a value of their studentID
(this is from the database and can't be hard coded FirstN: John LastN=Smith StudentID=1)
but then click the edit or search button and go from localhost/EditStudent
to localhost/student/edit/1
and it would take you to the "John Smith" edit page. 我需要帮助的问题是:我想将他们的名字设置为他们的
studentID
的值(这是从数据库中获取的,不能被硬编码为FirstN: John LastN=Smith StudentID=1)
,然后单击编辑或搜索按钮,然后从localhost/EditStudent
转到localhost/student/edit/1
,它将带您进入“ John Smith”编辑页面。
Thanks to anyone who takes the time to read this! 感谢所有花时间阅读本文的人!
Ok... Let me explain you the way that I know.. Maybe someone else will have a more simple way... 好吧...让我向您解释一下我所知道的方式。也许其他人会有更简单的方式...
I am explaining this without trying it in a code editor, So if there is any error that anyone notice, then please notify me. 我没有在代码编辑器中尝试就对它进行解释,因此,如果有人注意到任何错误,请通知我。
I hope you need to get the selected value to the post method in controller. 我希望您需要将选定的值添加到控制器中的post方法。 Consider we have a Model name as Students.
考虑我们有一个模型名称为学生。 So getting our job done We need to create a Model as
所以完成我们的工作,我们需要创建一个模型
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);
}
And in View 并在视野中
@Html.DropDownListFor(m=>m.SelectedStudentId , Model.StudentSelectList, "Select Student")
Then in controller the post method will be like. 然后在控制器中,post方法将类似。
[HttpPost]
public ActionResult StudentList(StudentListModel studentListModel)
{
int SelectedStudentValue = studentListModel.SelectedStudentId;
// Do other operations with the id
return View();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.