我有一个属性如下:
[DisplayName("Children")]
public List<long> ChildrenIds
{
get { return SelectedGroup.Children.Select(x => x.Id).ToList(); }
set
{
foreach (long groupId in value)
{
if (groupId != 0 && !SelectedGroup.Children.Select(x => x.Id).Contains(groupId))
{
var group = FactoryTools.Factory.Get<GroupLookup>(groupId);
if (group != null) SelectedGroup.AddChild(group);
}
}
var childrenLooper = SelectedGroup.Children.ToList();
foreach (var group in childrenLooper)
{
if (group.Id != 0 && !value.Contains(group.Id))
{
SelectedGroup.Children.Remove(group);
}
}
}
}
当我在我的标准中选择了至少 1 个孩子时,这很有效Razor Listbox
:
<div class="oneThird">
@Html.LabelFor(x => x.ChildrenIds)
@Html.ListBoxFor(x => x.ChildrenIds, ListHelper.GetGroupsMinusSelf(Model.SelectedGroup))
@Html.ValidationMessageFor(x => x.ChildrenIds)
</div>
但是,当我取消选择上面的所有项目时ListBox
,该属性的设置器永远不会被调用,因此这些项目永远不会被删除......确保所有项目都被删除的最佳方法是什么?