Jquery 不引人注目的验证在视图模型列表中的第一个项目之后不起作用 - c# MVC 4

Jquery Unobtrusive validation not working on items after first in a View Model list - c# MVC 4

所以基本上我有一个包含多个 "sections" 的表单,其中包含一些用户数据。用户可以 select 他们想提交哪些部分(每个部分都有一个复选框来指示这一点)。在呈现 html 并且不显眼的解析器具有 运行 之后出现问题。它仅使用客户端验证所需的必要数据属性填充第一部分的输入元素。我在下面包含了一些精简代码。

型号:

public class EnrollmentFormViewModel
{
    public List<EnrollmentLocation> LocationList { get; set; }

    public EnrollmentFormViewModel()
    {
        LocationList = new List<EnrollmentLocation>();
    }

    public class EnrollmentLocation
    {
        [DisplayName("Enroll Location")]
        public bool EnrollLocation { get; set; }

        [Required(ErrorMessage = "Please enter your first name")]
        [DisplayName("First Name")]
        public string FirstName { get; set; }

        public EnrollmentLocation()
        {
            EnrollLocation = true;
            TStatList = new List<SelectListItem>();

        }
    }

查看:

<div>
@using(@Html.BeginForm("EnrollUser", "EnrollUI"))
{        
    <div>
        @{ int count = 0;}
        @foreach (var station in Model.LocationList)
        {
            <div class="location form_new_row panel panel-primary">

                    <div id="info"class="form_half_width form_new_row">
                        <div id="first-name" class="q form_question form_half_width form_new_row">
                            @Html.LabelFor(m => station.FirstName, new { @class = "label-asterisk form_question_label" })
                            <div class="form_question_answer">
                                @Html.TextBoxFor(m => station.FirstName, new { @class = "text_field", Name= count + "FirstName" })
                            </div>
                            @Html.ValidationMessageFor(m => station.FirstName)
                        </div>
                    </div>
                    <div class="user-info form_half_width">
                        <div>@Html.CheckBoxFor(m => station.EnrollLocation)@Html.LabelFor(m => station.EnrollLocation, new { @class = "form_question_label" })</div>
                    </div>
            </div> 

            {
                count++;
            }
        }

        <div class="form_new_row">
            <input type="submit" class="submit_button" />
        </div>
    </div>
}

正如我之前所说,这正确填充了字段并且第一部分看起来不错。这是输入元素的示例:

<input name="0FirstName" class="text_field valid" data-val="true" 
 data-val-required="Please enter your first name" 
 id="FirstName" type="text" value="nothing">

然而,任何后续项目看起来像这样:

<input name="1FirstName" class="text_field valid" id="FirstName" 
 type="text" value="nothing">

我尝试了一些与其他帖子不同的方法,但没有任何效果。有人有建议吗?

看来您需要使用 for 循环而不是 foreach 遍历该列表。

@for(int i = 0; i < Model.LocationList.Count(); i++)
{
<div class="location form_new_row panel panel-primary">

    <div id="info" class="form_half_width form_new_row">
        <div id="first-name" class="q form_question form_half_width form_new_row">
            @Html.LabelFor(m => Model.LocationList[i].FirstName, new { @class = "label-asterisk form_question_label" })
            <div class="form_question_answer">
                @Html.TextBoxFor(m => Model.LocationList[i].FirstName, new { @class = "text_field", Name = count + "FirstName" })
            </div>
            @Html.ValidationMessageFor(m => Model.LocationList[i].FirstName)
        </div>
    </div>
    <div class="user-info form_half_width">
        <div>@Html.CheckBoxFor(m => Model.LocationList[i].EnrollLocation)@Html.LabelFor(m => Model.LocationList[i].EnrollLocation, new { @class = "form_question_label" })</div>
    </div>
</div>

{
    count++;
}
}  

这将为每个位置上的每个 属性 生成唯一的 ID 值。
例如。 LocationList[0].FirstName 将是第一个元素的 ID,而 LocationList[1].FirstName 将是第二个元素的名称。