简体   繁体   English

将新列表项添加到列表 <T> 在MVC,Razor,ASP.NET中使用表单

[英]Add new list items to a List<T> in MVC, Razor, ASP.NET with form

I am working on an order form. 我正在处理订单。 The page loads the previously saved order items into a table. 该页面将先前保存的订单项目加载到表中。 The first row of the table is the add new item row with Html.EditoFor() like this: 该表的第一行是使用Html.EditoFor()添加新项的行,如下所示:

@Html.EditorFor(m => m.OrderItems[0].Description)

Of course the [0] represents in this case nothing, this is just to demonstrate the goal. 当然,在这种情况下[0]代表什么,这仅仅是为了证明目标。 To show the existing items, a for loop is used: 为了显示现有项目,使用了for循环:

for (int i = 1; i < Model.OrderItems.Count; i++)
{ @Html.EditorFor(d => d.OrderItems[i].Description)}

The order items are stored in an IEnumerable list with four properties (Desc, Partnum, Cost, Qty). 订单项存储在具有四个属性(Desc,Partnum,Cost,Qty)的IEnumerable列表中。

The question is: the editor row is a form and when the user presses the Add button on the page how can that form add the new item to the existing list? 问题是:编辑器行是一个表单,当用户按下页面上的“添加”按钮时,该表单如何将新项添加到现有列表中?

您可以将“新项目”发布到另一个操作中,参数是“新项目”,您现在可以修改现有列表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM