Home Validation Message for Indexed fields for Loop in MVC 5 Razor
Reply: 0

Validation Message for Indexed fields for Loop in MVC 5 Razor

user3524
1#
user3524 Published in June 20, 2018, 10:43 pm

I'm new and currently using Razor MVC 5 and I just want to ask if how to render the Validation message that shows on the indexed EditorFor. Currently Added a Data Annotation [Required (ErrorMessage = "Email Address is required")] and [DataType(DataType.EmailAddress)] but it shows a validation like The Char field is required.

Heres my set of Codes.

@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>
}

Tried over riding the validation by adding new { htmlAttributes = new { @class = "form-control", required = "required", title = "Email is required" } })

but to no avail still shows the same vaidation error The Char field is required. Hope someone can help me with this

Edit: Here is my ModelView

[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; }
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.302036 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO