如何使用Asp.net MVC验证列表属性是否具有最小项目数(count = N)?

t-b*_*boy 5 validation asp.net-mvc binding

我有一个视图模型,其属性看起来像这样

Property SelectedGroups() as List(of string)
Run Code Online (Sandbox Code Playgroud)

在视图中我有这样的东西

<table>
    <tr>
        <th>Description</th>
    </tr>
    <tr>
        <td>
            <input type="hidden" name="SelectedGroups" value="one" />
            description one
        </td>
    </tr>
    <tr>
        <td>
            <input type="hidden" name="SelectedGroups" value="two" />
            description two
        </td>
    </tr>
    <tr>
        <td>
            <input type="hidden" name="SelectedGroups" value="three" />
            description three
        </td>
    </tr>
</table>
Run Code Online (Sandbox Code Playgroud)

使用jquery添加和删除表行.有没有办法在SelectedGroups属性上创建一个验证属性,该属性需要列表的最小项目数?这可以用javascript完成,但我希望它可以使用

<% Html.EnableClientValidation()%>
<%: Html.ValidationSummary(False)%>
Run Code Online (Sandbox Code Playgroud)

Dav*_*sky 2

您必须编写一个自定义验证器。内置的验证器并不那么复杂。

ScottGu 写了一篇关于自定义验证器的好文章:http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx