我有一个视图,我想在模型中迭代一个列表以呈现视图,当前代码类似于
<form method="Post">
<table class="table">
@Each
<tr>
<td>
<input type="hidden" name="Id[]" value="@Current.Id" />@Current.Id
</td>
<td>
<input type="text" name="Name[]" value="@Current.Name"/>
</td>
<td>
<input type="text" name="Code[]" value="@Current.Code"/>
</td>
<td>
<button type="submit">Submit</button>
</td>
</tr>
@EndEach
</table>
</form>
我真正想做的是
<form method="Post">
<table class="table">
@Each
<tr>
<td>
<input type="hidden" name="Id[@CurrentIndex]" value="@Current.Id" />@Current.Id
</td>
<td>
<input type="text" name="Name[@CurrentIndex]" value="@Current.Name"/>
</td>
<td>
<input type="text" name="Code[@CurrentIndex]" value="@Current.Code"/>
</td>
<td>
<button type="submit">Submit</button>
</td>
</tr>
@EndEach
</table>
</form>
@CurrentIndex 将插入列表索引。
然后可以使用列表索引在 POST 期间将属性绑定到列表,例如
Post["/"] = _ => {
var list = this.Bind<List<Model>>();
...... do something with list .........
}