在编辑和创建视图中保存 MultiSelectList 中的数据

发布于 2024-08-20 01:31:10 字数 2066 浏览 4 评论 0原文

我有3张桌子: 公司、 分包合同,以及 CompanyToSubcontract

CompanyToSubcontract 表是公司的指南和分包合同的指南。我在分包合同编辑和创建视图上有一个 MultiSelectList,用户可以在其中选择多个公司。我终于让它工作了,它显示了在分包编辑视图中选择的正确公司。我在保存数据方面没有问题。

在创建视图中,分包合同在写入Sql数据库之前没有guid,那么如何才能将guid保存到CompanyToSubcontract表中呢?

另外,在编辑视图中,我做错了一些事情。它不保存公司选择。我还需要删除未选择的公司的记录。这样做的最佳方法是什么?

我按照 NerdDinner 教程获得了基本结构,现在我正在尝试更新以满足我的需求。

谁能指出我正确的方向?

在 SubcontractRepository 中:

public void Save()
    {
        db.SubmitChanges();
    }

在控制器中:

 [AcceptVerbs(HttpVerbs.Post), Authorize]
    public ActionResult Edit(string id, FormCollection formValues)
    {
        // Retrieve existing subcontract
        subcontract subcontract = subcontractRepository.GetSubcontract(id);

        if (subcontract == null)
            return View("NotFound");
        else
        {
            try
            {
                UpdateModel(subcontract);

                IEnumerable<Guid> selectedCompanies = Request.Form["Companies"].Split(new Char[] { ',' }).Select(idStr => new Guid(idStr));

                foreach (var item in selectedCompanies)
                {
                    CompanyToSubcontract cs = new CompanyToSubcontract();
                    cs.subcontract_id = subcontract.subcontract_id;
                    cs.company_id = item;
                    subcontractRepository.Save();
                }


                subcontract.lastupdate_date = DateTime.Now;
                subcontract.lastupdatedby_user = User.Identity.Name;

                //Persist changes back to database
                subcontractRepository.Save();

                //Perform HTTP redirect to details page for the saved subcontract
                return RedirectToAction("Details", new { id = subcontract.subcontract_no });
            }
            catch
            {
                ModelState.AddRuleViolations(subcontract.GetRuleViolations());

                return View(new SubcontractFormViewModel(subcontract));
            }

        }
    }

I have 3 tables:
Companies,
Subcontracts, and
CompanyToSubcontract

The CompanyToSubcontract table is the guid of the Company and guid of the Subcontract. I have a MultiSelectList on the Subcontract Edit and Create views where the user can select multiple companies. I finally got it working where it displays the correct companies as selected in the Subcontract Edit view. I'm not having an issue with saving the data.

In the create view, the subcontract doesn't have a guid until it's written to the Sql database, so how can I get the guid to save to the CompanyToSubcontract table?

Also, in the edit view, I'm doing something wrong. It doesn't save.the company selections. I also need to delete records for companies which are unselected. What is the best way to go about doing that?

I followed the NerdDinner tutorial to get my basic structure and now I'm trying to update to fulfill my needs.

Anyone who can point me in the right direction?

In SubcontractRepository:

public void Save()
    {
        db.SubmitChanges();
    }

In Controller:

 [AcceptVerbs(HttpVerbs.Post), Authorize]
    public ActionResult Edit(string id, FormCollection formValues)
    {
        // Retrieve existing subcontract
        subcontract subcontract = subcontractRepository.GetSubcontract(id);

        if (subcontract == null)
            return View("NotFound");
        else
        {
            try
            {
                UpdateModel(subcontract);

                IEnumerable<Guid> selectedCompanies = Request.Form["Companies"].Split(new Char[] { ',' }).Select(idStr => new Guid(idStr));

                foreach (var item in selectedCompanies)
                {
                    CompanyToSubcontract cs = new CompanyToSubcontract();
                    cs.subcontract_id = subcontract.subcontract_id;
                    cs.company_id = item;
                    subcontractRepository.Save();
                }


                subcontract.lastupdate_date = DateTime.Now;
                subcontract.lastupdatedby_user = User.Identity.Name;

                //Persist changes back to database
                subcontractRepository.Save();

                //Perform HTTP redirect to details page for the saved subcontract
                return RedirectToAction("Details", new { id = subcontract.subcontract_no });
            }
            catch
            {
                ModelState.AddRuleViolations(subcontract.GetRuleViolations());

                return View(new SubcontractFormViewModel(subcontract));
            }

        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光磨忆 2024-08-27 01:31:11

在 selectedCompanies foreach 循环中,将:更改

cs.subcontract_id = subcontract.subcontract_id;

为:

cs.subcontract = subcontract;

其工作原理在我对 这个问题。此外,LINQ-to-SQL 还自动管理主键的分配及其到外键的传播。当您调用 db.SubmitChanges() 时,它会识别出如果分包对象没有分配外键,则会将其视为 INSERT 更改。此外,由于分包合同对象与 CompanyToSubcontract 对象关联,因此它知道用刚刚分配给分包合同实体的主键值更新关联实体中的外键字段。

In the selectedCompanies foreach loop, change:

cs.subcontract_id = subcontract.subcontract_id;

to:

cs.subcontract = subcontract;

The reason why this works is explained in detail in my response to this question. In addition, LINQ-to-SQL also manages the assignment of primary keys and their propagation to foreign keys automatically. When you call db.SubmitChanges(), it recognizes that if the subcontract object doesn't have a foreign key assigned, it treats it as an INSERT change. Also, since the subcontract object is being associated with a CompanyToSubcontract object, it knows to update the foreign key field in the association entity with the primary key value it just assigned to the Subcontract entity.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文