Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ForEach中的Radzen验证_C#_Razor_Blazor - Fatal编程技术网

C# ForEach中的Radzen验证

C# ForEach中的Radzen验证,c#,razor,blazor,C#,Razor,Blazor,我有一个Foreach,其中包含一个RadzenNumeric和它的验证。我遇到的问题是Foreach中的每个项目的名称和组件都是相同的。因此,如果十分之一有一个值,那么它们都不会显示验证错误 问题是,让这个名字独一无二的最佳方法是什么?有没有一种方法可以在Foreach中执行计数器,并将该值添加到名称和Componenet的末尾 @foreach (Models.Ingredients.MeasuredIngredient measuredIngredient in CompoundIngre

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

问题是,让这个名字独一无二的最佳方法是什么?有没有一种方法可以在Foreach中执行计数器,并将该值添加到名称和Componenet的末尾

@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>
}
@foreach(Models.components.measuredGrendient在componendElement.measuredGrendient中被测量)
{
}

应该可以对它们进行编号

@{ 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" ... />
}
@{var index=0;}
@foreach(模型.成分.测定成分.测定成分)
{
指数+=1;
}

Radzen名称和组件的问题在于它不支持复杂类型。根据亨克·霍尔特曼的建议,这里是如何让它工作的

@{
   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();
}
@{
int i=0;
字符串amount=“amount”+i.ToString();
string type=“type”+i.ToString();
字符串component=“component”+i.ToString();
}
@foreach(模型.成分.测定成分.测定成分)
{
i++;
amount=“amount”+i.ToString();
type=“type”+i.ToString();
component=“component”+i.ToString();
}

您是说您需要
金额
变量吗?或者这只是一种风格?因为
=“@(“Amount”+index)”
应该“正常工作”。是的,在正常的razor语法中可以工作。问题是Radzen不允许名称中包含复杂类型,因此@(“金额”+索引)会导致异常。因此,您需要在名称中声明变量之前设置它。复杂类型是什么意思?我刚刚做了
Test+2,但Test+variable不起作用。没有再次复制: