EntityFramework 和 MVC 视图(显示)

发布于 2024-11-07 22:33:22 字数 1726 浏览 0 评论 0原文

我有一个名为“项目”的主表。该项目具有外键,名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文