我的应用程序中有两个部分视图。一个包含选择列表以决定付款方式。如果用户选择直接付款,则会打开另一个部分,其中包含相关的输入字段和验证。如果他们选择检查此表单是否隐藏。但是,验证不会被删除
直接借记部分视图
<div class="form-horizontal">
<div class="form-group">
@Html.Label("Sort Code", new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(Model => Model.SortCode12, new { @class = "control-label col-md-1", @readonly = "readonly" })
<div class="col-md-1"> - </div>
@Html.TextBoxFor(Model => Model.SortCode34, new { @class = "control-label col-md-1", @readonly = "readonly" })
<div class="col-md-1"> - </div>
@Html.TextBoxFor(Model => Model.SortCode56, new { @class = "control-label col-md-1", @readonly = "readonly" })
</div>
<div class="col-md-10">
@Html.ValidationMessageFor(model => model.SortCode12, "", new { @class = "text-danger" })
@Html.ValidationMessageFor(model => model.SortCode34, "", new { @class = "text-danger" })
@Html.ValidationMessageFor(model => model.SortCode56, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.Label("Account Number", new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(Model => Model.BankAccountNumber, new { @class = "control-label col-md-2", @readonly = "readonly" })
</div>
<div class="col-md-10">
@Html.ValidationMessageFor(model => model.BankAccountNumber, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.Label("Account Name", new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(Model => Model.BankAccountName, new { @class = "control-label col-md-10", @readonly = "readonly" })
</div>
<div class="col-md-10">
@Html.ValidationMessageFor(model => model.BankAccountName, "", new { @class = "text-danger" })
</div>
</div>
付款方式部分视图
<div id="CurrentPaymentMethod">
<div class="panel-group">
<div class="panel panel-default">
<div class="cl panel-heading">
<h4 class="panel-title">
Payment Method
</h4>
</div>
<div class="panel-body">
<div class="form-group">
@Html.Label("Payment Method", new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(Model => Model.PaymentMethodID,
new SelectList(Model.PaymentMethods, "Id", "Description"),
"Select Payment Method",
new { @class = "form-control", @required = "required", @onchange = "ChangePaymentMethod(this.value)" })
</div>
<div class="col-md-10">
@Html.ValidationMessageFor(model => model.PaymentMethodID, "", new { @class = "text-danger" })
</div>
</div>
</div>
</div>
</div>
</div>
<script>
function ChangePaymentMethod(paymentMethodID)
{
@* Show Direct Debit section if the DD payment option (ID=0) has been selected*@
if (paymentMethodID == 0) {
document.getElementById("CurrentDirectDebitDetails").style.display = "block";
}
else {
document.getElementById("CurrentDirectDebitDetails").style.display = "none";
document.getElementById("SortCode34").removeAttribute("data-val-required");
document.getElementById("SortCode12").removeAttribute("data-val-required");
document.getElementById("SortCode56").removeAttribute("data-val-required");
document.getElementById("BankAccountNumber").removeAttribute("data-val-required");
document.getElementById("BankAccountName").removeAttribute("data-val-required");
}
}
在底部,我创建了一些javascript来显示和隐藏直接债务,具体取决于下拉选项。但这有效,但验证仍然是
对此有任何帮助将不胜感激
答案 0 :(得分:0)
您可以为所有验证消息提供特定的类名称,errorMessagesForm1 form2等... 然后使用这个类来选择消息并隐藏它或删除它或者你想用它做什么。
这样的事,
@Html.ValidationMessageFor(model => model.PaymentMethodID, "", new { @class = "text-danger ErrMsgsForm2" })
js
var ErrorMSGs= document.getElementsByClassName("ErrMsgsForm2");
// Array of all error messages, loop through it
for(var i = 0; i < ErrorMSGs.length; i++){
ErrorMSGs[i].style.visibility = "hidden"; // or any other method, maybe change innerHTML etc
}