asp.net-mvc - MVC 5 Razor 中循环的索引字段的验证消息
问题描述
我是新手,目前正在使用 Razor MVC 5,我只想问是否如何呈现在索引 EditorFor 上显示的验证消息。当前添加了数据注释[Required (ErrorMessage = "Email Address is required")]
,[DataType(DataType.EmailAddress)]
但它显示了类似的验证The Char field is required.
这是我的一组代码。
@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "createform" }))
var cnt = 0;
{
<div class="form-horizontal">
@Html.ValidationSummary(true)
@if (Model.AttendeeCount > 0 && Model != null)
{
for (int i = 0; i < Model.AdultCount; i++)
{
cnt++;
<div id="div_@cnt">
<div>Adult @i</div>
<div class="form-group">
@Html.LabelFor(model => model.FirstName[cnt], new { @class = "control-label col-md-2" })
<div class="col-md-6">
@Html.EditorFor(model => model.FirstName[cnt], new { htmlAttributes = new { @class = "form-control", required = "required", title = "First Name is required" } })
@Html.ValidationMessageFor(model => model.FirstName[cnt], "", new { @class = "text-danger" })
</div>
</div>
....
<div class="form-group">
@Html.LabelFor(model => model.Email[cnt], htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-6">
@Html.EditorFor(model => model.Email[cnt], new { htmlAttributes = new { @class = "form-control", required = "required", title = "Email is required" } })
@Html.ValidationMessageFor(model => model.Email[cnt], "", new { @class = "text-danger" })
</div>
</div>
</div>
}
}
</div>
}
尝试通过添加来进行验证 new { htmlAttributes = new { @class = "form-control", required = "required", title = "Email is required" } })
但无济于事仍然显示相同的验证错误The Char field is required.
希望有人可以帮助我
编辑:这是我的模型视图
[Required (ErrorMessage = "Last Name is required")]
[StringLength(50, ErrorMessage = "Last Name cannot be longer than 50 characters.")]
[Display(Name = "Last Name", Prompt = "Attendee Last Name")]
[DataType(DataType.Text)]
public string LastName { get; set; }
[Required(ErrorMessage = "First Name is required")]
[StringLength(50, ErrorMessage = "First Name cannot be longer than 50 characters.")]
[Display(Name = "First Name", Prompt = "Attendee First Name")]
[DataType(DataType.Text)]
public string FirstName { get; set; }
[Required(ErrorMessage = "PhoneNumber is required")]
[Display(Name = "Cell Phone")]
[DataType(DataType.PhoneNumber)]
public string CellPhone { get; set; }
[Required(ErrorMessage = "Email is required")]
[Display(Name = "Email")]
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
解决方案
推荐阅读
- c# - 在 asp.net core 3.1 中使用匿名用户 ID,例如 .Net 4.x 中的 Request.AnonymousID?
- javascript - TypeScript 重用字符串文字?
- actions-on-google - Google Smart Home - 报告状态实时 UI 更新
- python - 如何使用 ElementTree 在带有命名空间的 XML 文件中查找和编辑标签
- java - 根标签下具有不同元素名称和属性的 Jackson XML
- jquery - 通过悬停打开时保持引导 4 下拉链接的颜色
- angular - Angular如何在自定义ErrorHandler中使用HttpClient?
- python - 检查大写字母的最简单方法
- flutter - 在 null 上调用了 getter 'length'。接收方:null 尝试调用:长度
- winforms - 将多个表绑定到 Winform