How to use Asp.net MVC to check the properties of a list has a minimum number of elements (count = N)?

I have a view model that has a property similar to this

Property SelectedGroups() as List(of string)

In the view, I have something like this

<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>

Table rows are added and deleted using jquery. Is there a way to create a validation attribute for the SelectedGroups property that requires a minimum number of items for the list? This can be done using javascript, but I would like it to work with

<% Html.EnableClientValidation()%>
<%: Html.ValidationSummary(False)%>
+5
source share
1 answer

You will need to write a special validator. Built-in validators are not so complex.

ScottGu : http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx

+2

All Articles