.NET - How to validate a List with no values in the Model?

Viewed 22

I have a Model:

public class CreateViewModel.cs{
   public string Name {get; set;}
   public List<IFormFile>? Files {get; set;}
   public List<int>? Participants{get; set;}   
}

This Model will be used in the Create View. The Files and Participants can be null or up to a certain Count. The Participants are filled by DropDownBoxes:

ViewBag.Participants = new SelectList(userRepo.getAll().ToList()).Append(emptyItem);
....
   <div class="col">
<label asp-for="Participants" class="control-label"></label>
 <select asp-for="Participants" asp-items="ViewBag.Participants" class="form-control">       </select>
 <span asp-validation-for="Participants" class="text-danger"></span>
</div>
   <div class="col">
<label asp-for="Participants" class="control-label"></label>
 <select asp-for="Participants" asp-items="ViewBag.Participants" class="form-control">       </select>
 <span asp-validation-for="Participants" class="text-danger"></span>
</div>

I have eight of those Dropdowns and I want to read out their values from Participants in the CreateViewModel.

My problem is that the Model will not be valid if I don´t select all Dropdowns. When I select all eight Dropdowns it is.

-> How can I make a List validate as true even if it has no values? For normal types it is enough to just add an "?" after it´s Parameter... The same for "Files".

0 Answers
Related