0

I have a listboxfor as shown below:

 @Html.Label("Members", htmlAttributes: new { @class = "control-label required", @multiple = "multiple" })
 @Html.ListBoxFor(model => model.Members, (IEnumerable<SelectListItem>)ViewBag.Members, new { @class = "form-control", @multiple = "multiple" })                                               
 @Html.ValidationMessageFor(model => model.Members, "", new { @class = "text-danger" })

The problem that I am experiencing is that it does not show the validation message even if no member has been selected.

    [Required(ErrorMessage = "Please select a member")]
    public List<int> Members { get; set; }
Nate Pet
  • 44,246
  • 124
  • 269
  • 414

1 Answers1

0

If you check RequiredAttribute in reference source, you will see overriden IsValid method like this:

public override bool IsValid(object value) 
{
    // checks if the object has null value
    if (value == null) 
    {
        return false;
    }

    // other stuff

    return true;
}

The problem here is IsValid method only checks for null values & null objects, but does not check Count property which exists in collection objects e.g. IEnumerable<T>. If you want to check against zero-valued Count property (which indicates no selected items), you need to create custom annotation attribute inherited from RequiredAttribute containing IEnumerator.MoveNext() check and apply it to List<T> property:

[AttributeUsage(AttributeTargets.Property)]
public sealed class RequiredListAttribute : RequiredAttribute
{
    public override bool IsValid(object value)
    {
        var list = value as IEnumerable;

        // check against both null and available items inside the list
        return list != null && list.GetEnumerator().MoveNext();
    }
}

// Viewmodel implementation
public class ViewModel
{
    [RequiredList(ErrorMessage = "Please select a member")]
    public List<int> Members { get; set; }
}

Note:

Using int[] array type instead of List<int> e.g. public int[] Members { get; set; } should work for standard RequiredAttribute because array property returns null when no items are selected, while List<T> properties call default constructor which will create an empty list.

Related issue:

Required Attribute on Generic List Property

Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61