我設法讓它工作。
我的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
點。