首页 > 解决方案 > 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; }

标签: asp.net-mvcrazorasp.net-mvc-5

解决方案


推荐阅读