Olivier Muhring

Olivier Muhring

  • NA
  • 150
  • 10k

ASP .NET Core Model validation through services and with >1 parameter

Feb 5 2021 1:42 PM
For a project I have a web page which allows me to link people to specific juror functions for an assortment of "matches" that are being played on a given date.
Basically, the page shows a series of juror panels, each panel represents a specific game/match that is being helped on that day.
A panel consists of a listing of juror functions, the user then has to select a juror member for each function. The user can also leave a function unassigned, but each selected juror can only perform 1 task for the given day.
The required functions vary from game to game, so nothing of is it really fixed.
When they've selected the jurors and click on the "post" button the following model is sent back for processing:
  1. public class PanelModel  
  2. {  
  3.     #region Properties  
  4.     public int WedstrijdSessie { getset; }  
  5.     public string PanelName { getset; }  
  6.     public string ClubName { getset; }  
  7.     public List<JuryOpgaveProposition> PanelComposition { getset; }  
  8.     public SelectList JuryMembers { getset; }  
  9.    #endregion  
  10. }  
The Property PanelComposition consists of a list of the following objects: 
  1. public class JuryOpgaveProposition  
  2. {  
  3.     #region Properties  
  4.     public int? FK_WedstrijdSessie { getset; }  
  5.     public int? SK_JuryOpgave { getset; }  
  6.     public int? FK_Jury { getset; }  
  7.     public string Omschrijving { getset; }  
  8.     public string Functie { getset; }  
  9.     public string Naam { getset; }  
  10.     public string Voornaam { getset; }  
  11.     public string JuryNiveau { getset; }  
  12.     public int? JuryNiveauNr { getset; }  
  13.     public int? SK_Club { getset; }  
  14.     public string Club { getset; }  
  15.     public int? Vlg_PositieToegekend { getset;}  
  16.     #endregion  
  17.  
  18.     #region Calculated properties  
  19.     public string JuryName => $"{Voornaam} {Naam}";  
  20.     #endregion  
  21. }  
Anyway, the only data they can change on the page are the functions, which are being represented here by a listing of JuryOpgaveProposition-objects.
Now... before I can accept this I have - of course - to validate the data they can be sent:
- Each juror has to be unique in the panel.
- Each juror has to be free for being selected.
The listing being proposed to the user is a listing of the jurors that are available for that day, but some of them may already be judging a different game.
To make it more user friendly validation errors have to be shown on the screen, right next to the selected juror.
I know I can do this with ASP .NET validation, but I'm struggling a bit.
Problem 1:
I need to create custom validators which allows me to display an error for each juror, but most of the examples I have found build a listing of errors which I can then show on the top or the bottom of the screen. In this case however I want to be able to show the error right next to the validated fields, like I would be able to do with the built-in validators (the one for credit card numbers for example).
Problem 2:
Both validators require me to access the database.
And... all my database access is currently being handled through the use of services. The examples I've found assume the entities exist within the ASP .NET project and are accessed there. Which is not the idea here. How do I get past this?
Problem 3:
Both validators would need the queries to filter on multiple parameters. The examples I've found don't show how I can pass extra parameters to the validator.
I hope anyone here can help me. :-)

Answers (2)