我设法让它工作。
我的jQuery模板:
function loadWagons(trainId, partId) {
$.getJSON('/train/wagons/' + escape(trainId) + '?partNo=' + partId, function (data) {
$wagons = $('#wagons tbody');
$wagons.empty();
// the function used in the template to get an index.
var tmplOptions = {
getIndex: function getIndex() {
return $.inArray(this.data, data);
}
};
$("#wagonTemplate").tmpl(data, tmplOptions).appendTo($wagons);
});
}
换句话说:加载模板
<script id="wagonTemplate" type="text/x-jquery-tmpl">
<tr>
<td>
<input type="checkbox" value="true" class="wagoncheck" name="wagons[${$item.getIndex()}].IsSelected" />
</td>
<td>
<input type="text" name="wagons[${$item.getIndex()}].WagonId" value="${WagonId}" style="width:120px" />
</td>
<td>
<input type="text" name="wagons[${$item.getIndex()}].WagonNumber" value="${WagonNumber}" style="width:20px" />
</td>
</script>
方法
为了让在你的控制器动作YourModel[] items
参数,你需要命名作为items[0].MyProperty' where
0`的项目应该对应于数组中的索引。
要获取jQuery模板中的索引,只需在模板函数的选项中使用传递方法即可。我正在使用一个稍微修改后的版本,发现here。在该答案中完成项目并不需要,因为当前项目上的this
点。