【发布时间】:2016-10-27 14:46:17
【问题描述】:
所以我有一个这样工作的表: 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 和 lines-1、lines-2、lines-3)
迭代这些文本框、保存它们的值(我可以在保存之前处理验证)然后将其传递到后端的最佳方法是什么?
【问题讨论】:
-
请注意,您的代码将永远无法正确进行双向绑定,您需要一个具有 2 个属性(
int Hid和int Line)的模型集合,然后正确生成视图并允许正确的绑定和验证,参考this answer
标签: javascript c# forms razor asp.net-mvc-5