2017-07-23 75 views
0

我在我的應用程序中有兩個部分視圖。其中一個包含一個選擇列表以決定付款方式。如果用戶選擇直接借記,則另一部分將打開相關輸入字段和驗證。如果他們選擇檢查這個表單是隱藏的。不過驗證不除從另一個局部視圖刪除字段的驗證

直接借記管窺

<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

使用[萬無一失](http://foolproof.codeplex.com/)'[RequiredIf]'或施加類似條件驗證屬性到您的屬性 –

回答

0

你可以給特定的類名進行的所有驗證消息,errorMessagesForm1窗口2等... 然後使用這個類來選擇信息和隱藏或刪除它或任何你想做的事情。

這樣的事情,

@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 
    } 
+0

這將無法正常工作 - 模型仍然無效,表單無法提交 - 但知道用戶沒有最隱祕的想法發生了什麼,因爲沒有錯誤可見 –

+0

@StephenMuecke我的觀點是隱藏錯誤的驗證他不是的形式使用,而不是所有的,一旦我們回到這種形式,他應該再次展示它。 這是個壞主意嗎? – Munzer

+1

是的。隱藏錯誤文本並不會阻止'$ .validator'無效(並且因爲它無效,表單將不會被提交回服務器,並且窮人不知道發生了什麼)。 OP需要使用條件驗證屬性 –

相關問題