[英]Jquery Unobtrusive validation not working on items after first in a View Model list - c# MVC 4
因此,基本上,我有一个表单,其中包含多个“节”,其中包含一些用户数据。 用户可以选择他们要提交的部分(每个部分都有一个复选框来表明这一点)。 在呈现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值。
EG LocationList [0] .FirstName将是第一个元素的ID,而LocationList [1] .FirstName将是第二个元素的名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.