我有一个行为几乎相同的应用程序并选择了相同的技术。但是,我的 ViewModel 同时包含 List1 和 List2。
调用我的编辑器模板是这样完成的:
<div id="SoftwarePanel" class="collapsiblePanel">
<h2>Request Software</h2>
@Html.EditorFor(m => m.Software)
</div>
在这种情况下,“软件”是“RequestableList”类的一个实例,其中包含“AvailableItems”和“RequestedItems”的属性。编辑器模板使两个列表彼此相邻,放置适当的按钮用于在列表框之间来回移动项目并连接 javascript。
<table>
<tr>
<td>
<input type="text" id="avail@{@name}Filter" />
<button type="button" id="avail@{@name}Clear" class="DualList_availClear dualListButton"">
X</button><br />
@Html.ListBoxFor(m => m.AvailableItems, new MultiSelectList((from i in Model.AvailableItems where !Model.RequestedItems.Contains(i.Key) select i), "Key", "Value"), new { @class = "dualList" })
<br />
<span id="avail@{@name}Counter" class="countLabel"></span>
<select id="avail@{@name}Storage">
</select>
</td>
<td>
<button id="to2@{@name}" type="button" class="dualListButton">
></button><br />
<button id="allTo2@{@name}" type="button" class="dualListButton">
>></button><br />
<button id="allTo1@{@name}" type="button" class="DualList_Allto1 dualListButton">
<<</button><br />
<button id="to1@{@name}" type="button" class="dualListButton">
<</button><br />
</td>
<td>
<input type="text" id="@{@name}RequestedFilter" />
<button type="button" id="@{@name}RequestedClear" class="DualList_requestClear dualListButton">
X</button><br />
@Html.ListBoxFor(m => m.RequestedItems, new MultiSelectList((from i in Model.RequestedItems select new { key = i, value = Model.AvailableItems[i] }).AsEnumerable(), "key", "value"), new { @class = "dualList" })
<br />
<span id="@{@name}RequestedCounter" class="countLabel"></span>
<select id="@{@name}RequestedStorage">
</select>
</td>
</tr>
</table>
更新:我使用了 Justin Mead 开发的 DualListBox 插件。 http://www.meadmiracle.com/dlb/DLBDocumentation.aspx
<script type="text/javascript">
$(function () {
$.configureBoxes({
box1View: '@ViewData.TemplateInfo.GetFullHtmlFieldId("AvailableItems")',
box1Storage: 'avail@{@name}Storage',
box1Filter: 'avail@{@name}Filter',
box1Clear: 'avail@{@name}Clear',
box1Counter: 'avail@{@name}Counter',
box2View: '@ViewData.TemplateInfo.GetFullHtmlFieldId("RequestedItems")',
box2Storage: '@{@name}RequestedStorage',
box2Filter: '@{@name}RequestedFilter',
box2Clear: '@{@name}RequestedClear',
box2Counter: '@{@name}RequestedCounter',
to1: 'to1@{@name}',
to2: 'to2@{@name}',
allTo1: 'allTo1@{@name}',
allTo2: 'allTo2@{@name}',
onItemsChanged: function () {
var opts = $('#@ViewData.TemplateInfo.GetFullHtmlFieldId("RequestedItems") option');
var optIds = $.map(opts, function(e) { return $(e).val(); });
}
});
});
</script>