ForEach 中的 Radzen 验证

Radzen Validation in a ForEach

我有一个包含 RadzenNumeric 和验证的 Foreach。我遇到的问题是 Foreach 中的每个项目的名称和组件都相同。因此,如果 10 个中有 1 个有值,那么它们都不会显示验证错误。

问题是,使名称独一无二的最佳方法是什么?有没有办法在 Foreach 中做一个计数器,然后将该值添加到名称和组件的末尾?

@foreach (Models.Ingredients.MeasuredIngredient measuredIngredient in CompoundIngredient.MeasuredIngredients)
{
    <div class="ingredient-seperator"></div>
    <div class="row ingredient">
        <div class="amountInput col-6 col-md-2">
            <RadzenNumeric class="newInput" Name="Amount" @bind-Value="measuredIngredient.Amount" TValue="decimal" Min="0"></RadzenNumeric>
            <RadzenRequiredValidator class="rz-message rz-messages-error rz-message-popup newInputValidation" Component="Amount" Text="Amount is required" DefaultValue="0" Popup="true" />
        </div>
        
    </div>
}

应该可以给它们编号。

@{ var index = 0; }

@foreach (Models.Ingredients.MeasuredIngredient measuredIngredient in CompoundIngredient.MeasuredIngredients)
{
    index += 1;
    <RadzenNumeric class="newInput" Name="@("Amount"+index)" @bind-Value="measuredIngredient.Amount" TValue="decimal" Min="0"></RadzenNumeric>
    <RadzenRequiredValidator class="rz-message ..." Component="@("Amount"+index)" Text="Amount is required" ... />
}

Radzen 名称和组件的问题在于它不支持复杂类型。根据 Henk Holtermans 的建议,这里是如何让它工作的

@{
   int i = 0;
   string amount = "Amount" + i.ToString();
   string type = "Type" + i.ToString();
   string ingredient = "Ingredient" + i.ToString();
}
@foreach (Models.Ingredients.MeasuredIngredient measuredIngredient in CompoundIngredient.MeasuredIngredients)
{
    <div class="ingredient-seperator"></div>
    <div class="row ingredient">
         <div class="amountInput col-6 col-md-2">
               <RadzenNumeric class="newInput" Name="@(amount)" @bind-Value="measuredIngredient.Amount" TValue="decimal" Min="0"></RadzenNumeric>
               <RadzenRequiredValidator class="rz-message rz-messages-error rz-message-popup newInputValidation" Component="@(amount)" Text="Amount is required" DefaultValue="0" Popup="true" />
         </div>
    </div>

   i++;
   amount = "Amount" + i.ToString();
   type = "Type" + i.ToString();
   ingredient = "Ingredient" + i.ToString();
}