如何使用DataAnnotations验证IEnumerable不为null或为空?

时间:2016-10-21 19:23:11

标签: asp.net-mvc-4

我正在尝试验证IEnumerable<long> selectedItems如果不为空或空但我不能这样做,它需要选择一个或多个选项,因为它是一个多选项。我怎么能这样做?

尝试。

模型

public class EmpresaModel{

    public IEnumerable<SelectListItem> formasPagto { get; set; }

    [Required(ErrorMessage = "Choose one or more options")]
    public IEnumerable<long> selectedItems { get; set; }
}

HTML

<div class="form-group">
         <label for="@Html.IdFor(model => model.selectedItems)" class="cols-sm-2 control-label">Formas de pagamento disponíveis <img src="~/Imagens/required.png" height="6" width="6"></label>
           @Html.ListBoxFor(model => model.selectedItems, Model.formasPagto, new { Class = "form-control", placeholder = "Selecione as formas de pagamento disponíveis", @multiple = true})
           @Html.ValidationMessageFor(model => model.formasPagto)
</div>

0 个答案:

没有答案