ASP.NET MVC 主详细信息输入表
我正在尝试使用 ASP.NET MVC 实现订单输入表单,但面临很多困难。我找到的所有示例都与查看主详细信息表单相关,没有一个用于添加或编辑。
假设我有两个表:Order 和 OrderLines,它们以一对多关系相互关联。在主视图中,我有一个“新建”按钮,单击它应该显示一个由订单字段组成的新订单视图,一个显示订单行的网格,以及一个“保存”按钮,单击该按钮将保留整个订单以及将其行存入数据库。网格应该有三个按钮:“添加行”、“编辑行”和“删除行”。单击“添加行”时,应显示一个新视图,允许用户将行添加到订单视图网格线中 - 在此阶段数据库不受影响 -。当用户单击“编辑线”时,将显示一个视图,允许用户编辑所选线,并在完成后更新订单网格线。
最困难的问题是:
如何在订单视图和订单行视图之间传递订单及其行集合?
如何根据订单行视图的变化更新订单视图?
以及如何在不涉及数据库的情况下保留视图之间的更改?
有没有一个具体的例子展示如何使用MVC来实现这个?
感谢您的帮助和反馈。
I’m trying to implement an order entry form using ASP.NET MVC but facing a lot of difficulties. All the samples that I found are related to viewing master detail forms, and none for adding or editing.
Suppose I have two tables: Order and OrderLines that are related to each others with one-to-many relationship. In the main view I had a “New” button when clicked it should show a new order view composed of the order fields, a grid that shows the order lines, and a “Save” button that when clicked will persist the whole order along with its lines into a database. The grid should have three buttons: “Add Line”, “Edit Line”, and “Delete Line”. When the “Add Line” is clicked a new view should be shown that allows the user to add the line to the order view grid lines –at this stage the database is not affected-. When the user clicks “Edit Line” a view will be shown that allows the user to edit the selected line and when done update the order grid lines.
The most difficult problems are:
How to pass the order and its lines collection between the order view and the order line views?
How to update the order view based on changes in the order line view?
And how to persist changes between views without the database being involved?
Is there a concrete example that shows how to implement this using MVC?
Your help and feedback is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
请查看我的 博客发布关于在 ASP.NET MVC 中创建主详细信息表单的文章。它还包含您可以下载的演示项目
Pleas have a look at my blog post on creating master detail form in asp.net mvc. it also contains demo project that you can download
与 WebForms 不同,ASP.NET MVC 不会尝试隐藏 HTTP 的无状态性质。要跨多个表单处理复杂对象,您有几个选择:
我通常自己使用客户端选项,主表单具有将在子表单中编辑的数据的隐藏字段 不过,您可能会发现服务器端选项更容易 - 如果您确实不想涉及数据库,则可以将部分更新的对象保留在会话中。
Unlike WebForms, ASP.NET MVC does not try to hide the stateless nature of HTTP. To work with a complex object across multiple forms you have a couple of options:
I usually go with the client side option myself, with the main form having hidden fields for the data that will be edited in the subform. You may find the server side option easier though - if you really don't want to involve the database you can keep your partially updated object in the session.
第 1 步:创建视图模型
第 2 步:添加用于添加订单行的 javascript
步骤 3:创建一个保存数据的操作,
您可以下载源代码和视频教程
Step 1: Create view model
Step 2: Add javascript for add order lines
Step 3: Create an action for save data
you can download source code and video tutorial
您可以尝试 Telericks 免费的 MVC 网格控件...
http://demos.telerik.com/ aspnet-mvc/grid/hierarchyserverside
You could try Telericks free MVC grid control...
http://demos.telerik.com/aspnet-mvc/grid/hierarchyserverside
就在我的头顶上(一种大脑垃圾场)...
你可以有一个表单的主网格部分。这将是从操作加载的完整视图(带有订单号或不带有订单号,具体取决于是否加载现有订单号)。
单击事件(新建或编辑)时,可能会以“灯箱”样式打开部分视图。然后,这会将 json 对象传回主窗体。
然后,传递的 json 对象将使用模板呈现到表底部(对于新表)或更新现有记录。这也可以在同一个 ajax 调用中保存回服务器。或者只是更新客户端并需要用户单击保存按钮。
将需要一个 isDirty 标志,以便任何更改将其设置为 true,并且当浏览器尝试离开或关闭等时,您可以提示用户保存或不保存。
希望这有帮助。
编辑
没有尝试过这个,但您的问题的无数据库方面可能会很有趣点击
Just off the top of my head (a kind of brain dump)...
You could have a main grid part of the form. This would be full view loaded from an action (either with an order number or not depending on loading an existing one or not).
When clicking an event (new or edit) this could open a partial view in a "lightbox" style. This would then pass back a json object back to the main form.
The passed json object would then be rendered using templating to the bottom of the table (for a new one) or update an existing record. This could also be saved back to the server in the same ajax call. Or just update the client side and need the user to click a save button.
An isDirty flag will be needed so any changes set it to true and the when the browser tries to leave or close etc. then you can prompt the user to save or not.
Hope this helps.
edit
Not tried this but could be interesting with the none db aspect of your question click
步骤 3:创建保存数据的操作。
[http邮报]
Step 3: Create an action for save data.
[HttpPost]