条件不显眼的验证模式
Conditionally unobtrusive validation mode
我正在使用 Razor、C# 和 .NET Framework 4.7 开发 ASP.NET MVC 5 应用程序。
我想根据模型的 属性 值禁用 table 上的某些输入字段。我是用 javascript:
做的
if ($('#LawId').val() === "1") {
$('#productsTable').attr("disabled", true);
$('.addLevelButton').attr("disabled", true);
$('.deleteLevelButton').attr("disabled", true);
}
这非常有效,但我不知道如何禁用非侵入式验证。剃刀视图是:
<td>
<div class="group">
@Html.TextBoxFor(m => m.Products[index].Name, new { @class = "productClass" })<br />
<div class="mensajeError">@Html.ValidationMessageFor(m => m.Products[index].Name)</div>
</div>
</td>
<td>
<div class="group">
@Html.TextBoxFor(m => m.Products[index].Description, new { @class = "productClass", @style = "max-width:none" })<br />
<div class="mensajeError">@Html.ValidationMessageFor(m => m.Products[index].Description)</div>
</div>
</td>
<td>
<div class="group">
@Html.TextBoxFor(m => m.Products[index].Comment, new { @class = "productClass" })<br />
<div class="mensajeError">@Html.ValidationMessageFor(m => m.Products[index].Comment)</div>
</div>
</td>
<td>
<div class="group">
@Html.CheckBox(string.Format("Delete_{0}", index))
</div>
</td>
这是为我要隐藏的字段生成的 HTML(它们都在 productsTable
table 内)。
<div class="group">
<input data-val="true" data-val-number="The field Id must be a number." data-val-required="The Id field is required." id="Products_0__Id" name="Products[0].Id" type="hidden" value="0" />
<input data-val="true" data-val-number="The field Law must be a number." data-val-required="The Law field is required." id="Products_0__Law" name="Products[0].Law" type="hidden" value="1" />
<input data-val="true" data-val-length="Must be 14 characters long" data-val-length-max="14" data-val-length-min="14" data-val-required="Product's code is requiered" id="Products_0__ProductCode" name="Products[0].ProductCode" type="number" value="" />
<div class="mensajeError"><span class="field-validation-valid" data-valmsg-for="Products[0].ProductCode" data-valmsg-replace="true"></span></div>
</div>
搜索我找到了帮助程序@{ Html.EnableClientValidation(false); }
,但我不知道如何使用它,也没有找到任何完整的示例。
我在 Razor 视图中有条件地显示输入文件:
@if (Model.LawId == 1)
{
<input name = "ChinaCodes" id = "ChinaCodes" class="upload" type="file">
}
我是否必须对每个字段都执行相同的操作,或者我是否可以同时对所有字段执行全局操作?
我已经在 @body
部分的开头添加了这个:
@section Body
{
@if (Model.LawId == 1)
{
Html.EnableClientValidation(false);
Html.EnableUnobtrusiveJavaScript(false);
}
[ ... ]
}
我正在使用 Razor、C# 和 .NET Framework 4.7 开发 ASP.NET MVC 5 应用程序。
我想根据模型的 属性 值禁用 table 上的某些输入字段。我是用 javascript:
做的if ($('#LawId').val() === "1") {
$('#productsTable').attr("disabled", true);
$('.addLevelButton').attr("disabled", true);
$('.deleteLevelButton').attr("disabled", true);
}
这非常有效,但我不知道如何禁用非侵入式验证。剃刀视图是:
<td>
<div class="group">
@Html.TextBoxFor(m => m.Products[index].Name, new { @class = "productClass" })<br />
<div class="mensajeError">@Html.ValidationMessageFor(m => m.Products[index].Name)</div>
</div>
</td>
<td>
<div class="group">
@Html.TextBoxFor(m => m.Products[index].Description, new { @class = "productClass", @style = "max-width:none" })<br />
<div class="mensajeError">@Html.ValidationMessageFor(m => m.Products[index].Description)</div>
</div>
</td>
<td>
<div class="group">
@Html.TextBoxFor(m => m.Products[index].Comment, new { @class = "productClass" })<br />
<div class="mensajeError">@Html.ValidationMessageFor(m => m.Products[index].Comment)</div>
</div>
</td>
<td>
<div class="group">
@Html.CheckBox(string.Format("Delete_{0}", index))
</div>
</td>
这是为我要隐藏的字段生成的 HTML(它们都在 productsTable
table 内)。
<div class="group">
<input data-val="true" data-val-number="The field Id must be a number." data-val-required="The Id field is required." id="Products_0__Id" name="Products[0].Id" type="hidden" value="0" />
<input data-val="true" data-val-number="The field Law must be a number." data-val-required="The Law field is required." id="Products_0__Law" name="Products[0].Law" type="hidden" value="1" />
<input data-val="true" data-val-length="Must be 14 characters long" data-val-length-max="14" data-val-length-min="14" data-val-required="Product's code is requiered" id="Products_0__ProductCode" name="Products[0].ProductCode" type="number" value="" />
<div class="mensajeError"><span class="field-validation-valid" data-valmsg-for="Products[0].ProductCode" data-valmsg-replace="true"></span></div>
</div>
搜索我找到了帮助程序@{ Html.EnableClientValidation(false); }
,但我不知道如何使用它,也没有找到任何完整的示例。
我在 Razor 视图中有条件地显示输入文件:
@if (Model.LawId == 1)
{
<input name = "ChinaCodes" id = "ChinaCodes" class="upload" type="file">
}
我是否必须对每个字段都执行相同的操作,或者我是否可以同时对所有字段执行全局操作?
我已经在 @body
部分的开头添加了这个:
@section Body
{
@if (Model.LawId == 1)
{
Html.EnableClientValidation(false);
Html.EnableUnobtrusiveJavaScript(false);
}
[ ... ]
}