これは古い質問だと思いますが、これを回避する方法は、ビュー モデルに 2 つの IEnumerable メンバーを含めることでした (私のモデルでは List<> を使用していました)。最初のメンバーは選択するリストで、2 番目は選択肢の受信者で、2 つのボックスの間にボタンがあり、選択したアイテムの移動を管理します。
public class DataViewModel
{
// some other model members
public List<SelectListItem> SourceList { get; set; }
public List<SelectListItem> DestinationList { get; set; }
}
私のコントローラーでは、両方のメンバーの新しいリストを作成しました
DataViewModel vm = new DataviewModel() {
SourceList = new List<SelectListItem>(),
DestinationList= new List<SelectListItem>(),
// Other initialization
}
次に、使用しているソースからソース リストを作成します (私のアプリケーションでは、Active Directory からユーザーのリストを取得していました)。アプリケーションが現在の選択肢を表示してそれらを削除するか、単に表示することを許可している場合は、宛先リストに入力することもできます。ただし、リストへの入力は必須ではありません。
次に、私の見解では、適切なバインディングを使用して両方のリストを追加しました。
@model Application.WebApp.Models.DataViewModel
<!-- Some HTML/Razor Markup -->
<div class="form-group">
<div class="col-sm-3">
@Html.ListBoxFor(m => m.SourceList, Model.SourceList, new {size = 10})
</div>
<div class="col-sm-1">
<input type="submit" class="btn" value="-->>" name="Action"/>
<input type="submit" class="btn" value="-->" name="Action"/>
<input type="submit" class="btn" value="<--" name="Action"/>
<input type="submit" class="btn" value="<<--" name="Action"/>
</div>
<div class="col-sm-3">
@Html.ListBoxFor(m => m.DestinationList, Model.DestinationList, new {size = 10})
</div>
</div>
<!-- Some HTML/Razor Markup -->
ビューについては、目的に合わせてマークアップをいじる必要があるでしょう。