C# 使用不引人注目的验证验证动态添加的字段集时出现问题

C# 使用不引人注目的验证验证动态添加的字段集时出现问题,c#,javascript,jquery,asp.net-mvc-3,C#,Javascript,Jquery,Asp.net Mvc 3,我目前正在一个需要根据用户输入动态添加/更改视图的站点上工作。根据用户在下拉框中选择的内容,我在页面上呈现部分视图。对于大多数视图,这是可行的;但是,我有一个局部视图,可以选择生成多个字段集。当只有一个字段集时(阅读:用户不选择添加更多字段集的选项),不引人注目的验证有效,但当用户生成更多字段集时,仅对第一个字段集启动验证。在浏览了一下这里之后,我发现了Xhalent关于动态不引人注目的验证的帖子,以及一篇给了我实际复制字段集的逻辑的帖子。不幸的是,我对JS/jQuery还相当陌生(不好),还没

我目前正在一个需要根据用户输入动态添加/更改视图的站点上工作。根据用户在下拉框中选择的内容,我在页面上呈现部分视图。对于大多数视图,这是可行的;但是,我有一个局部视图,可以选择生成多个字段集。当只有一个字段集时(阅读:用户不选择添加更多字段集的选项),不引人注目的验证有效,但当用户生成更多字段集时,仅对第一个字段集启动验证。在浏览了一下这里之后,我发现了Xhalent关于动态不引人注目的验证的帖子,以及一篇给了我实际复制字段集的逻辑的帖子。不幸的是,我对JS/jQuery还相当陌生(不好),还没有弄清楚如何将这两个想法很好地结合起来

下面是我用来复制表单字段的jQuery代码(我没有包括实际字段,因为这实际上是一个概念问题,在这一点上,字段的内容是不相关的。另外,我不知道如何在这里格式化它们):

(注意:itemCountVal是用户选择的编号1-6)

以下是我保存在JS文件(Dupe.JS)中的函数:

我知道:

$.validator.unobtrusive.parseDynamicContent('form input:last');  
或者说是某种变体,必须要去某个地方,但我不知所措

问题:如何将Xhalent的精细验证方法合并到复制字段集的代码中

编辑:

以下是页面上引用的脚本(第1.cshtml节):


以下是要复制的表格:

@using (Html.BeginForm("Section1","P15",FormMethod.Post))
{
@Html.ValidationSummary(true)
<fieldset>
<legend>FILL ME OUT FIRST!</legend>
    <div class="PrimaryOPSelector">
        OP Number (This is your Primary OP, or the OP that you would be changing shifts from):
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(model => model.OP_Number, new SelectList(Model.Ops, "Op_Number", "Op_Number"))
        @Html.ValidationMessageFor(model => model.OP_Number)
    </div>
<p>Please select the number of shifts you would like to have off/change with another staff member: <select id="itemCountSec1" name="itemCountSec1">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
</select></p>
<div class="ReasonForRequest">
        Please Select The Reson For Your Request For Time Off:
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(model => model.Reason_For_Request, new SelectList(Model.ReasonsForRequest, "Reason_ID", "Reason_For_Request1"))
        @Html.ValidationMessageFor(model => model.Reason_For_Request)
    </div>
</fieldset>  
<fieldset id="Section1Fields">

       <p><strong>Shift Start Date: </strong>@Html.DropDownListFor(model => model.Month, new SelectList(Model.Months, "Month_Value", "Month_Name"))
        @Html.DropDownListFor(model => model.Day, new SelectList(Model.Days))
        @Html.DropDownListFor(model => model.Year, new SelectList(Model.Years))</p>

<p><strong>Start Time Of Shift: </strong>
@Html.DropDownListFor(model =>model.Start_Hour, new SelectList(Model.Hours)) :
@Html.DropDownListFor(model => model.Start_Min, new SelectList(Model.Minutes))
@Html.DropDownListFor(model => model.Start_Marker, new SelectList(Model.AMPM))
</p>


<p><strong>End Time Of Shift: </strong>
@Html.DropDownListFor(model => model.End_Hour, new SelectList(Model.Hours)) :  
@Html.DropDownListFor(model => model.End_Min, new SelectList(Model.Minutes))
@Html.DropDownListFor(model => model.End_Marker, new SelectList(Model.AMPM))
</p>


<p><strong>Covering Staff Employee Number: </strong>@Html.EditorFor(model => model.Covering_Staff_Employee_Num)</p>
@Html.ValidationMessageFor(model => model.Covering_Staff_Employee_Num)

<p><strong>Covering Staff Phone Number: </strong>@Html.EditorFor(model => model.Covering_Staff_Phone)</p>
@Html.ValidationMessageFor(model => model.Covering_Staff_Phone)

<p><strong>Type Of Time Off: </strong>@Html.DropDownListFor(model => model.Type_Of_Time_Off, new SelectList(Model.Types, "Type_ID", "Name"))</p>
@Html.ValidationMessageFor(model => model.Type_Of_Time_Off)

<p><strong>Number Of Hours To Be Used: </strong>@Html.EditorFor(model => model.Hrs_Used)</p>
@Html.ValidationMessageFor(model => model.Hrs_Used)
</fieldset>

<p><input type="submit" value="Submit this section" /></p>
}
@使用(Html.BeginForm(“第1节”,“第15页”,FormMethod.Post))
{
@Html.ValidationSummary(true)
先填我!
OP编号(这是您的主要OP,或您将从中换班的OP):
@DropDownListFor(model=>model.OP\u编号,新选择列表(model.Ops,“OP\u编号”,“OP\u编号”))
@Html.ValidationMessageFor(model=>model.OP_编号)
请选择您希望与另一名员工休假/换班的班次:
1.
2.
3.
4.
5.
6.

请为您的请假请求选择原因: @Html.DropDownListFor(model=>model.Reason\u For\u请求,新选择列表(model.ReasonsForRequest,“Reason\u ID”,“Reason\u For\u请求1”)) @Html.ValidationMessageFor(model=>model.Reason用于请求) 轮班开始日期:@Html.DropDownListFor(model=>model.Month,新选择列表(model.Months,“Month\u Value”,“Month\u Name”)) @Html.DropDownListFor(model=>model.Day,新选择列表(model.Days)) @Html.DropDownListFor(model=>model.Year,新选择列表(model.Years))

班次开始时间: @Html.DropDownListFor(model=>model.Start_Hour,新选择列表(model.Hours)): @Html.DropDownListFor(model=>model.Start\u Min,新选择列表(model.Minutes)) @DropDownListFor(model=>model.Start\u标记,新选择列表(model.AMPM))

班次结束时间: @Html.DropDownListFor(model=>model.End_Hour,新选择列表(model.Hours)): @Html.DropDownListFor(model=>model.End\u Min,新选择列表(model.Minutes)) @DropDownListFor(model=>model.End_标记,新的SelectList(model.AMPM))

覆盖员工编号:@Html.EditorFor(model=>model.Covering\u Staff\u Employee\u Num)

@Html.ValidationMessageFor(model=>model.Covering\u Staff\u Employee\u Num) 覆盖员工电话号码:@Html.EditorFor(model=>model.Covering\u Staff\u Phone)

@Html.ValidationMessageFor(model=>model.Covering\u Staff\u Phone) 休息时间类型:@Html.DropDownListFor(model=>model.Type\u Of\u Time\u Off,新选择列表(model.Types,“Type\u ID”,“Name”))

@Html.ValidationMessageFor(model=>model.Type\u Of\u Time\u Off) 要使用的小时数:@Html.EditorFor(model=>model.Hrs\u已使用)

@Html.ValidationMessageFor(model=>model.Hrs\u使用)

}
与其使用如此复杂的方法,不如在加载动态内容后调用的函数中使用以下三行代码:

$("form").removeData("validator");
$("form").removeData("unobtrusiveValidation");
$.validator.unobtrusive.parse("form");

这将清除页面上以前解析过的内容,允许再次解析页面上的所有内容,并将验证应用于所有元素。

我尝试在Dupe.js中if语句的末尾(就在.find(“input”)调用之后)以及$(文档)中实际函数调用的末尾添加该代码.ready()节。在这两种情况下,用于验证的文本以及字段的颜色都有效,但它会在两个字段集(包括在加载中的字段集以及动态添加的字段集)上显示验证。最重要的是,即使其中一个框留空,表单仍然允许帖子通过。想法,还是我需要提供更多信息?我愿意坐在这里一整天,直到我明白这一点。还有,我忘了说,但谢谢你。我是我们公司唯一的开发人员,因此很难从其他人那里获得想法。请添加您的表单以及要克隆的内容。编辑初始帖子以包含要克隆的表单。您是否也在
\u Layout.cshtml
文件中引用脚本?如果没有,那么添加脚本的顺序就有问题,而且顺序很重要。此外,您的设计还为访问控制器上POST操作中的数据设置了一些重大问题。在您当前的级别上,您可能承担的任务过于复杂,无法完成单个页面的任务。我建议将此任务分成多个较小的页面。
$.validator.unobtrusive.parseDynamicContent('form input:last');  
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>  
<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.js")" type="text/javascript"></script>  
<script src="@Url.Content("~/Scripts/Dupe.js")" type="text/javascript"></script>  
@using (Html.BeginForm("Section1","P15",FormMethod.Post))
{
@Html.ValidationSummary(true)
<fieldset>
<legend>FILL ME OUT FIRST!</legend>
    <div class="PrimaryOPSelector">
        OP Number (This is your Primary OP, or the OP that you would be changing shifts from):
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(model => model.OP_Number, new SelectList(Model.Ops, "Op_Number", "Op_Number"))
        @Html.ValidationMessageFor(model => model.OP_Number)
    </div>
<p>Please select the number of shifts you would like to have off/change with another staff member: <select id="itemCountSec1" name="itemCountSec1">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
</select></p>
<div class="ReasonForRequest">
        Please Select The Reson For Your Request For Time Off:
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(model => model.Reason_For_Request, new SelectList(Model.ReasonsForRequest, "Reason_ID", "Reason_For_Request1"))
        @Html.ValidationMessageFor(model => model.Reason_For_Request)
    </div>
</fieldset>  
<fieldset id="Section1Fields">

       <p><strong>Shift Start Date: </strong>@Html.DropDownListFor(model => model.Month, new SelectList(Model.Months, "Month_Value", "Month_Name"))
        @Html.DropDownListFor(model => model.Day, new SelectList(Model.Days))
        @Html.DropDownListFor(model => model.Year, new SelectList(Model.Years))</p>

<p><strong>Start Time Of Shift: </strong>
@Html.DropDownListFor(model =>model.Start_Hour, new SelectList(Model.Hours)) :
@Html.DropDownListFor(model => model.Start_Min, new SelectList(Model.Minutes))
@Html.DropDownListFor(model => model.Start_Marker, new SelectList(Model.AMPM))
</p>


<p><strong>End Time Of Shift: </strong>
@Html.DropDownListFor(model => model.End_Hour, new SelectList(Model.Hours)) :  
@Html.DropDownListFor(model => model.End_Min, new SelectList(Model.Minutes))
@Html.DropDownListFor(model => model.End_Marker, new SelectList(Model.AMPM))
</p>


<p><strong>Covering Staff Employee Number: </strong>@Html.EditorFor(model => model.Covering_Staff_Employee_Num)</p>
@Html.ValidationMessageFor(model => model.Covering_Staff_Employee_Num)

<p><strong>Covering Staff Phone Number: </strong>@Html.EditorFor(model => model.Covering_Staff_Phone)</p>
@Html.ValidationMessageFor(model => model.Covering_Staff_Phone)

<p><strong>Type Of Time Off: </strong>@Html.DropDownListFor(model => model.Type_Of_Time_Off, new SelectList(Model.Types, "Type_ID", "Name"))</p>
@Html.ValidationMessageFor(model => model.Type_Of_Time_Off)

<p><strong>Number Of Hours To Be Used: </strong>@Html.EditorFor(model => model.Hrs_Used)</p>
@Html.ValidationMessageFor(model => model.Hrs_Used)
</fieldset>

<p><input type="submit" value="Submit this section" /></p>
}
$("form").removeData("validator");
$("form").removeData("unobtrusiveValidation");
$.validator.unobtrusive.parse("form");