Ich habe zwei Teilansichten in meiner Anwendung. Einer enthält eine Auswahlliste, um zwischen Zahlungsmethoden zu entscheiden. Wählt der Benutzer Lastschrift, öffnet sich ein anderer Teil mit den entsprechenden Eingabefeldern und Validierung. Wenn sie auswählen, wird dieses Formular ausgeblendet. Allerdings ist die Validierung nichtEntfernen Sie die Validierung für Felder aus einer anderen Teilansicht
Lastschriftteilansicht
<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>
Zahlungsmethode Teilansicht
<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");
}
}
entfernt
Am unteren ich einige Javascript erstellt haben, um zu zeigen, und verstecken die direkte Schulden von abhängig von der Drop-Down-Auswahl. Dies funktioniert jedoch die Validierung bleibt
Jede mögliche Hilfe bei diesem würde
Verwenden Sie einen [narrensicher] (http://foolproof.codeplex.com/) '[RequiredIf]' oder ähnliches bedingte Validierung Attribut angewendet zu Ihren Eigenschaften –