In my ASP.NET MVC application I have a View with a select-tag with the ability to select multiple options.
By default (when the page loads) I have no options in the select. From some action the select can be populated with options and this works fine.
Now when I submit the form I want to check if the select has any options or not, as it is required that the select has at least one option when the form is submitted. I don't want to check whether any options are selected or not, just if the select has any options. This is what I've got so far:
<select multiple="multiple" style="width:200px;" id="PersonnelClasses"
name="PersonnelClasses"></select>
And the jQuery-code:
$("#PersonnelClasses").rules("add", {
required: true
});
Now, what will the rule look like to check if the select has any items in its list? I haven't found any solution here on SO, nor in the jQuery docs.
Update:
Code for adding options to the select:
this.addPersonnelClass = function () {
$('#selectablePersonnelClasses :selected').
each(function (value, item) {
$('#PersonnelClasses')
.append($('<option></option>')
.val(item.value)
.html(item.text));
});
};
selecttag looks like? What about your controller and the rest of your view. Is there something that dynamically adds more options to the select?selectoptions change?