我在我的應用程序中有兩個部分視圖。其中一個包含一個選擇列表以決定付款方式。如果用戶選擇直接借記,則另一部分將打開相關輸入字段和驗證。如果他們選擇檢查這個表單是隱藏的。不過驗證不除從另一個局部視圖刪除字段的驗證
直接借記管窺
<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來顯示和隱藏直接負債取決於下拉選擇項目。然而,這作品的驗證仍然
任何幫助,將不勝感激
使用[萬無一失](http://foolproof.codeplex.com/)'[RequiredIf]'或施加類似條件驗證屬性到您的屬性 –