所以我有一张像这样工作的桌子:https://gfycat.com/WeakBlueIslandwhistler
产生于:
<table class="table table-bordered table-with-button table-condensed " id="hidTable">
<thead>
<tr>
<th>HID #</th>
<th>Lines</th>
</tr>
</thead>
@for (int j = 0; j < 3; j++)
{
<tr>
<td>
<input class="form-control table-with-button" type="number" placeholder="HID" id="hid-@j">
</td>
<td>
<input class="form-control table-with-button" type="number" placeholder="Lines" id="lines-@j">
</td>
</tr>
}
</table>新行和文本框是通过调用javascript方法创建的。
实际上,这个表中有一个未知数量的文本字段对,数据对需要传递给控制器.(我正在考虑将它作为对象存储在tempdata中?)
每个文本框都有一个唯一的id (hid-1,hid-2 hid-3和行-1,行-2,行-3)。
在这些文本框上迭代、保存它们的值(我可以在保存之前处理验证)并将其传递给后端的最佳方法是什么?
发布于 2016-10-27 15:23:28
如果满足某些条件,MVC Modelbinder将能够直接绑定POST数据:
id属性的值应该是{collectionName}_0,name属性值应该是{collectionName}[0]。因此,在您的例子中,定义一个包含HID和行列表的ViewModel
public class PostDataModel {
public ICollection<int> Hids { get; set; }
public ICollection<int> Lines { get; set; }
}然后确保添加额外行的javascript代码正确设置id和name。
第0行生成的输入如下所示:
<input class="form-control table-with-button" type="number" placeholder="HID" id="Hids_0" name="Hids[0]">
<input class="form-control table-with-button" type="number" placeholder="Lines" id="Lines_0" name="Lines[0]">如果用户在求和前可以删除任何行,请注意non sequential indices!
然后,只需提交带有普通POST的表单,并使用它们的索引关联HID和Line。
https://stackoverflow.com/questions/40287678
复制相似问题