I have a model which should be validated.
public class VersandModalInput
{
public Courier Courier { get; set; } = Courier.UPS;
public List<PaketInput> Pakete { get; set; } = new List<PaketInput>();
public int AnzahlPakete { get; private set; } = 0;
public bool MailSenden { get; set; } = true;
public void AddPaket(decimal gewicht = 0)
{
Pakete.Add(new PaketInput { Paketnummer = ++AnzahlPakete, Gewicht = gewicht });
}
public void RemovePaket()
{
Pakete.RemoveAt(--AnzahlPakete);
}
public void RemovePaket()
{
Pakete.RemoveAt(--AnzahlPakete);
}
}
My Model as a List. Here is the class:
public class PaketInput
{
public int Paketnummer { get; set; }
[Required]
[Range(0.1, 31)]
public decimal Gewicht { get; set; }
public string Trackingnummer { get; set; } = ""; // This should be required is Courier of VersandModalInput is Manual
public string MbeTrackingnummer { get; set; } = "";
}
I want to have the VersandModalInput become invalid if the Courier of it is Manual and any of the trackingnumbers of from List pakete is empty. The packages are displayed in the frontend and the error should be visible right under the specific field. This is my UI for pakete:
<table class="table table-responsive-xs">
<thead>
<tr>
<th>Paket</th>
<th>Gewicht (Kg)</th>
@if (BelegVersandInput.Versandtyp == Versandtyp.Manuell)
{
<th>Trackingnummer</th>
}
</tr>
</thead>
<tbody>
@foreach (var paket in BelegVersandInput.Pakete)
{
<tr>
<td>@paket.Paketnummer</td>
<td>
<InputNumber @bind-Value="paket.Gewicht" class="form-control" />
<ValidationMessage For="() => paket.Gewicht" />
</td>
@if (BelegVersandInput.Versandtyp == Versandtyp.Manuell)
{
<td>
<InputText @bind-Value="paket.Trackingnummer" class="form-control" />
<ValidationMessage For="() => paket.Trackingnummer" />
</td>
}
</tr>
}
</tbody>
Is there any way to recrete this kind of validation with the classes specified above?