【问题标题】:asp-validation-for not working under foreach due to same name issue由于同名问题,asp-validation-for 不能在 foreach 下工作
【发布时间】:2020-09-20 08:19:23
【问题描述】:

我在 foreach 下使用 asp-validation-for 并面临以下问题。假设使用 foreach 生成 3 个文本控件。我正在使用 [required] 模型注释。 “objGenExaminationTemplateChoicesModel”对象是主模型中的ICollection。

  1. 如果第一个文本控件为空,系统将显示错误消息 所有 3 个文本控件。

  2. 如果第二个或第三个是空系统不是 显示任何消息但由于型号原因不继续发布页面 错误。

    <table id="dtChoices" class="table table-borderless table-striped">
    <thead class="bg-primary">
        <tr>
            <th class="text-left"><label asp-for="@Model.objGenExaminationTemplateChoicesModel.FirstOrDefault().ChoiseDescription" class="control-label"></label></th>
            <th style="width:30px"><a href="#" id="AddNewChoice" class="text-warning"><span class="sidenav-icon icon icon-plus-square pull-right" style="font-size:large">&nbsp;</span></a></th>
        </tr>
    </thead>
    <tbody>
        @foreach (var objGenExaminationTemplateChoiceModel in Model.objGenExaminationTemplateChoicesModel)
        {
            iCounter = iCounter + 1;
            <tr>
                <td class="text-left form-group form-group-sm">
                    <input type="text" asp-for="@objGenExaminationTemplateChoiceModel.ChoiseDescription" class="form-control">
                    <span asp-validation-for="@objGenExaminationTemplateChoiceModel.ChoiseDescription" class="text-danger"></span>
                </td>
                <td>
                    <a href="#" class="text-warning btnDeleteRow" data-toggle="modal" data-target="#deleteConfirmationModalAlert">
                        <span class="sidenav-icon icon icon-trash pull-right" style="font-size:large">&nbsp;</span>
                    </a>
                </td>
    
            </tr>
        }
    </tbody>
    

【问题讨论】:

  • 我也尝试了以下代码行但不起作用。

标签: asp.net-core


【解决方案1】:

objGenExaminationTemplateChoicesModel 是一个列表模型,因此模型绑定系统会通过[i].propertyName 找到名称。更改您的代码如下:

@model TestVmodel
<form>
    <table id="dtChoices" class="table table-borderless table-striped">
        <thead class="bg-primary">
            <tr>
                <th class="text-left"><label asp-for="@Model.objGenExaminationTemplateChoicesModel.FirstOrDefault().ChoiseDescription" class="control-label"></label></th>
                <th style="width:30px"><a href="#" id="AddNewChoice" class="text-warning"><span class="sidenav-icon icon icon-plus-square pull-right" style="font-size:large">&nbsp;</span></a></th>
            </tr>
        </thead>
        <tbody>
            @{ var iCounter = 0;}
            @for (int i = 0; i < Model.objGenExaminationTemplateChoicesModel.Count(); i++)
            {
                iCounter = iCounter + 1;
                <tr>
                    <td class="text-left form-group form-group-sm">
                        <input type="text" asp-for="@Model.objGenExaminationTemplateChoicesModel[i].ChoiseDescription" class="form-control">
                        <span asp-validation-for="@Model.objGenExaminationTemplateChoicesModel[i].ChoiseDescription" class="text-danger"></span>
                    </td>
                    <td>
                        <a href="#" class="text-warning btnDeleteRow" data-toggle="modal" data-target="#deleteConfirmationModalAlert">
                            <span class="sidenav-icon icon icon-trash pull-right" style="font-size:large">&nbsp;</span>
                        </a>
                    </td>

                </tr>
            }
        </tbody>
    </table>
    <input type="submit" value="aa" />
</form>
@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

我的测试模型:

public class TestVmodel
{
    public List<ObjGenExaminationTemplateChoicesModel> objGenExaminationTemplateChoicesModel { get; set; }
}
public class ObjGenExaminationTemplateChoicesModel
{
    [Required]
    public string ChoiseDescription { get; set; }
}

结果:

【讨论】:

  • 谢谢好答案。您的示例中的命名很糟糕,并且不需要 iCounter,但想法很好:)
猜你喜欢
  • 1970-01-01
  • 2017-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-02
  • 1970-01-01
  • 2019-03-09
  • 1970-01-01
相关资源
最近更新 更多