在保存父对象之前,如何(以及在​​哪里)将子对象从 UI (jqgrid) 存储到 asp.net-mvc 服务器

发布于 2024-11-09 13:36:43 字数 1857 浏览 3 评论 0原文

我有一个 asp.net-mvc 站点,我使用 nhibernate。我遇到了一个问题,我的 UI 需要将子项添加到父对象,但该父对象尚未插入(因此没有 ID),

详细信息如下:

我有一个基本的“详细信息”页面这是一个表单(一堆输入控件),代表一个对象(在本例中为 Project 对象)的属性,其中有一个用于名称的文本框、用于描述的文本区域等。 。

(简化视图)项目对象看起来像这样,

public class Project
{
      public string Name;
      public string Description;
      public List<Project> Dependencies;
      public int Id;
}

我重用此屏幕来添加新的项目(其中所有控件在启动时都是空白的
我重用此屏幕来编辑现有的项目(我从服务器加载给定项目 ID 的所有控件),

现在的问题是添加和删除依赖项项目的 UI到项目(列表)的依赖关系属性。它有点复杂,但我不能只使用常规的多选列表框,因为需要搜索依赖项(因为有数千个依赖项)

我决定用来显示依赖项列表的 UI 是 jqGrid。

在此处输入图像描述

当您单击 jqgrid 的“添加”按钮时,它会弹出一个小对话框来添加依赖项

< img src="https://i.sstatic.net/pJtZM.png" alt="在此处输入图像描述">

当您选择一个项目时,它会调用服务器向该项目添加依赖项,关闭弹出窗口并单击然后刷新“依赖网格”以显示最新的依赖项目列表。

这是我的添加依赖项代码:

public ActionResult AddDepedency(Dependency dependency)
{
      var project = GetProjectFromRepository(dependency.ProjectId);
      var projectDependency = GetProjectFromRepository(dependency.DependencyProjectId);

      project.Dependencies.Add(projectDependency);
      this.Repository.Save(project);
      this.Repository.Commit();
}

顺便说一下,供参考,这是我的 ProjectMap 类中用于流畅的 nhibernate 连接的关系:

  HasMany(x => x.Dependencies).AsBag().Inverse().Cascase.AllDeleteOrphan().Fetch.Select().BatchSize(80);

当我在编辑屏幕上执行此操作时,效果很好,因为我已经有一个项目 ID。

但我不知道如何让它在“添加项目”屏幕上工作,因为人们会在保存初始项目本身之前尝试添加对项目的依赖项,因此它还没有 id< /强>。

我应该使用一些会话缓存来临时存储此列表吗?因此,当我实际提交项目进行插入时,在项目插入成功后,我会将当时的所有依赖项附加到主项目中?

一种选择是,我可以根本不支持在“添加”项目页面上添加依赖项,而只在“编辑”项目页面上支持它,但这似乎会让用户感到烦恼。

有没有人以前遇到过这个问题并可以提供任何建议?

I have an asp.net- mvc site and I use nhibernate. I am running into an issue where my UI needs to add child items to a parent object but that parent object has yet to be inserted (and thus doesn't have an ID)

Here are the details:

I have a basic "detail" page that is a form (a bunch of input controls) that represent the properties of an object (a Project object in this case) where a have a textbox for Name, textarea for description, etc . .

A (simplified view) Project object looks like this

public class Project
{
      public string Name;
      public string Description;
      public List<Project> Dependencies;
      public int Id;
}

i reuse this screen for adding a new Project (where all the controls are blank on startup
i reuse this screen for editing existing Project (where i load all of controls from the server given a project Id)

the issue is now around the UI of adding and removing Dependencies items to the Dependency property of a Project (a list). Its a little complicated but I cant just use a regular multiselect listbox because there needs to be a search for a dependency (as there are thousands of them)

The UI that i decided to use to show the list of dependencies is a jqGrid.

enter image description here

When you click on the Add button of the jqgrid, it pops up a little dialog to add a dependency

enter image description here

when you choose a project, it then calls the server to add a dependency to the project, close the popup and then refresh the "Dependency grid" to show you the latest list of dependency projects.

Here is my add dependency code:

public ActionResult AddDepedency(Dependency dependency)
{
      var project = GetProjectFromRepository(dependency.ProjectId);
      var projectDependency = GetProjectFromRepository(dependency.DependencyProjectId);

      project.Dependencies.Add(projectDependency);
      this.Repository.Save(project);
      this.Repository.Commit();
}

by the way for reference, here is the relationship in my ProjectMap class for fluent nhibernate wiring:

  HasMany(x => x.Dependencies).AsBag().Inverse().Cascase.AllDeleteOrphan().Fetch.Select().BatchSize(80);

This works fine when i am doing this for the edit screen AS I ALREADY HAVE A PROJECT ID.

but i can't figure out how to get this to work on the ADD project screen because people would be trying to add a dependency to a project BEFORE the initial project itself is saved so it doesn't yet have an id.

should i use some session cache to temporarily store this list? so when i actually go to commit the project for insert, at that point i them attach all of the dependencies at that time to the main project after the project insert succeeds ?

one option, is that I could simply not support adding dependencies on the ADD project page and only support it on the EDIT project page but that seems like it will be annoying to the users.

Has anyone run into this issue before and could offer any suggestions?

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

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

发布评论

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

评论(1

半窗疏影 2024-11-16 13:36:43

就我个人而言,我会将新的依赖项存储到 HTML 输入字段中,并且仅当用户提交表单时将它们全部发送到服务器,服务器将它们插入到数据库中。这是一个 不错的博客帖子说明了这个概念。

Personally I would store the new dependencies into HTML input fields and only when the user submits the form send them all to the server which will insert them into the database. Here's a nice blog post illustrating the concept.

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