Вы можете написать скрипт для проверки.Проверьте, можно ли запретить удаление длины select == длины выбранного.
$('#moveLeft').click(function () {
var leftselected = $('#leftSelectednumbers').val();
var length = $('#leftSelectednumbers option').length;
//alert(length)
var selected = $('#leftSelectednumbers :selected').length;
if (length === selected) {
alert('Can not move all item to right...');
} else {
$.each(leftselected, function (key, value) {
$('#rightSelectednumbers').append($("<option></option>").attr("value", value).text(value));
});
$('#leftSelectednumbers :selected').remove();
}
})
cshtml файл
@model TestMVC.Controllers.NumberClass
@using (Html.BeginForm())
{
<div class="col-md-6" style="font-family:Arial">
@Html.ListBoxFor(m => m.leftSelectednumbers, Model.leftnumbers, new { size = 20, @class = "listBox" })
<br />
<input type="button" id="moveLeft" value="move left" />
</div>
<div class="col-md-6" style="font-family:Arial">
@Html.ListBoxFor(m => m.rightSelectednumbers, new List<SelectListItem>(), new { size = 20, @class = "listBox" })
<br />
<input type="button" value="move right" />
</div>
}
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script>
$('#moveLeft').click(function () {
var leftselected = $('#leftSelectednumbers').val();
var length = $('#leftSelectednumbers option').length;
//alert(length)
var selected = $('#leftSelectednumbers :selected').length;
if (length === selected) {
alert('Can not move all item to right...');
} else {
$.each(leftselected, function (key, value) {
$('#rightSelectednumbers').append($("<option></option>").attr("value", value).text(value));
});
$('#leftSelectednumbers :selected').remove();
}
})
</script>
Обновление скрипта для сортировки после перемещения
@model TestMVC.Controllers.NumberClass
@using (Html.BeginForm())
{
<div class="col-md-6" style="font-family:Arial">
@Html.ListBoxFor(m => m.leftSelectednumbers, Model.leftnumbers, new { size = 20, @class = "listBox" })
<br />
<input type="button" id="moveLeft" value="move left" />
</div>
<div class="col-md-6" style="font-family:Arial">
@Html.ListBoxFor(m => m.rightSelectednumbers, new List<SelectListItem>(), new { size = 20, @class = "listBox" })
<br />
<input type="button" value="move right" />
</div>
}
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script>
$('#moveLeft').click(function () {
var leftselected = $('#leftSelectednumbers').val();
var length = $('#leftSelectednumbers option').length;
//alert(length)
var selected = $('#leftSelectednumbers :selected').length;
if (length === selected) {
alert('Can not move all item to right...');
} else {
var currentrightvalue = [];
$('#rightSelectednumbers option').each(function (item, option) {
debugger
currentrightvalue.push(option.value);
});
leftselected = leftselected.concat(currentrightvalue);
leftselected.sort(function(a,b) { return a - b; });
$('#rightSelectednumbers option').remove();
$.each(leftselected, function (key, value) {
$('#rightSelectednumbers').append($("<option></option>").attr("value", value).text(value));
});
$('#leftSelectednumbers :selected').remove();
}
})
</script>