EntityFramework 和 MVC 视图(显示)
我有一个名为“项目”的主表。该项目具有外键,名为 DeptId(部门 ID)、SiteId(站点 ID)。当视图显示时,我想显示部门表中的部门名称和站点表中的站点名称,但是当数据保存回项目表时,我想将它们的 ID 保存回Project 表作为外键。 因此,我想在视图中显示 Department 表和 Site 表中的名称,但当视图中的数据保存回数据库时,我想将它们的相关 ID 保存到 Project 表中。我应该如何使用 EF 来做到这一点?处理一张表很容易,但我不清楚如何渲染一个字段以进行显示并将另一字段(ID)保存到主项目数据库。谢谢
----更新------
我找到了一个我需要做什么的例子,但是你能告诉我这个下拉列表是如何填充的吗?下拉列表中的第一个参数是否与控件中的 ViewBag 匹配?
--从视图--
@Html.DropDownList("DepartmentID", String.Empty)
@Html.ValidationMessageFor(model => model.DepartmentID)
--从控制器--
[HttpPost]
public ActionResult Create(Course course)
{
try
{
if (ModelState.IsValid)
{
unitOfWork.CourseRepository.Insert(course);
unitOfWork.Save();
return RedirectToAction("Index");
}
}
catch (DataException)
{
//Log the error (add a variable name after DataException)
ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator.");
}
PopulateDepartmentsDropDownList(course.DepartmentID);
return View(course);
private void PopulateDepartmentsDropDownList(object selectedDepartment = null)
{
var departmentsQuery = unitOfWork.DepartmentRepository.Get(
orderBy: q => q.OrderBy(d => d.Name));
ViewBag.DepartmentID = new SelectList(departmentsQuery, "DepartmentID", "Name", selectedDepartment);
}
I have a main table called Projects. This project has foreign keys on it named DeptId for department ID, SiteId for a Site ID. When the View gets displayed, I want to show the name of the department from the Department table and the name of the Site from the Site table, but when the data is saved back to the Project table, I want to save their IDs back to the Project table as the foreign key.
So, I want to display the names from the Department table and from the Site table in the View but I want to save their related IDs to the Project table when the data in the View is saved back to the database. How should I go about doing this with EF? It is easy to deal with one table but I am not clear on how to render one field for display and save another field (ID) to the main project database. Thanks
----UPDATE------
I found an example of what I need to do but can you tell me how this dropdownlist gets populated. Does the first parameter in the dropdownlist match the ViewBag in the coltroller?
--FROM VIEW--
@Html.DropDownList("DepartmentID", String.Empty)
@Html.ValidationMessageFor(model => model.DepartmentID)
--FROM CONTROLLER--
[HttpPost]
public ActionResult Create(Course course)
{
try
{
if (ModelState.IsValid)
{
unitOfWork.CourseRepository.Insert(course);
unitOfWork.Save();
return RedirectToAction("Index");
}
}
catch (DataException)
{
//Log the error (add a variable name after DataException)
ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator.");
}
PopulateDepartmentsDropDownList(course.DepartmentID);
return View(course);
private void PopulateDepartmentsDropDownList(object selectedDepartment = null)
{
var departmentsQuery = unitOfWork.DepartmentRepository.Get(
orderBy: q => q.OrderBy(d => d.Name));
ViewBag.DepartmentID = new SelectList(departmentsQuery, "DepartmentID", "Name", selectedDepartment);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
本教程介绍了这种情况:
http://www.asp.net/entity-framework/tutorials/updating-lated-data-with-the-entity-framework-in-an-asp-net-mvc-application
This scenario is covered in this tutorial:
http://www.asp.net/entity-framework/tutorials/updating-related-data-with-the-entity-framework-in-an-asp-net-mvc-application