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; }
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 eg 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>
eg public int[] Members { get; set; }
public int[] Members { get; set; }
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:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.