Asp.net core 由于同名问题,无法在foreach下工作的asp验证
我正在为UnderForeach使用asp验证,并面临以下问题。假设使用foreach生成3个文本控件。我正在使用[required]模型注释。“ObjGenExamizationTemplateChoicesModel”对象在主模型中是ICollection对象Asp.net core 由于同名问题,无法在foreach下工作的asp验证,asp.net-core,Asp.net Core,我正在为UnderForeach使用asp验证,并面临以下问题。假设使用foreach生成3个文本控件。我正在使用[required]模型注释。“ObjGenExamizationTemplateChoicesModel”对象在主模型中是ICollection对象 如果第一个文本控件为空,系统将显示错误消息 所有3个文本控件 如果第二个或第三个为空,则系统不可用 显示任何消息,但由于型号原因,不继续发布页面 错误 @foreach(模型中的var objGenExaminationTempl
@foreach(模型中的var objGenExaminationTemplateChoiceModel.objGenExaminationTemplateChoiceModel)
{
i计数器=i计数器+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"> </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"> </span>
</a>
</td>
</tr>
}
</tbody>
</table>
<input type="submit" value="aa" />
</form>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
@model TestVmodel
@{var iCounter=0;}
@对于(int i=0;i
我的测试模型:
public class TestVmodel
{
public List<ObjGenExaminationTemplateChoicesModel> objGenExaminationTemplateChoicesModel { get; set; }
}
public class ObjGenExaminationTemplateChoicesModel
{
[Required]
public string ChoiseDescription { get; set; }
}
公共类TestVmodel
{
公共列表对象检查模板选择模型{get;set;}
}
公共类对象检查模板选择模型
{
[必需]
公共字符串选择说明{get;set;}
}
结果:
我也尝试了以下代码行,但不起作用。
public class TestVmodel
{
public List<ObjGenExaminationTemplateChoicesModel> objGenExaminationTemplateChoicesModel { get; set; }
}
public class ObjGenExaminationTemplateChoicesModel
{
[Required]
public string ChoiseDescription { get; set; }
}