title: Required field validator for a textbox in MVC depend on a dynamically generated drop down list
body: <p>I'm generating some controls with the help of a for-each loop. I would like to validate the textbox depend on the answer of the dropdown. For some questions, validator should be enabled if the selected item in dropdown is "yes" and for some questions validator should be enabled if the answer from the dropdown is "no". At the moment it fires for both because I don't how to control it as the controls are dynamically generated</p>
  
  <p><strong>View</strong></p>
  
  <pre><code>        @for (int a = 0; a &lt; Model.ServiceQaVisits.Count(); a++)
          {
              if (Model.ServiceQaQuestions[a].sqqQuestionID == Model.ServiceQaVisits[a].sqvQuestionID)
              {
                  &lt;div&gt;
                      @Html.DisplayTextFor(m =&gt; m.ServiceQaVisits[a].sqvQuestionID)
                      @Html.DisplayTextFor(m =&gt; m.ServiceQaQuestions[a].sqqQuestion)
                      @Html.HiddenFor(m =&gt; m.ServiceQaVisits[a].sqvQAID)
                      @if (Model.ServiceQaQuestions[a].sqqQuestionTypeID == 1)
                      {
  
                          @Html.TextBoxFor(m =&gt; m.ServiceQaVisits[a].sqvAnswer)
                      }
                      else if (Model.ServiceQaQuestions[a].sqqQuestionTypeID == 2)
                      {
                          List&lt;string&gt; lista = new List&lt;string&gt;() { "Yes", "No" };
                          @Html.DropDownListFor(m =&gt; m.ServiceQaVisits[a].sqvAnswer, new SelectList(lista), "Select Answer")
                      }
                      else
                      {
                          List&lt;string&gt; listb = new List&lt;string&gt;() { "Yes", "No", "N/A" };
                          @Html.DropDownListFor(m =&gt; m.ServiceQaVisits[a].sqvAnswer, new SelectList(listb), "Select Answer")
                      }
                      @if (Model.ServiceQaQuestions[a].sqqNegativeAnswer != null)
                      {
                          @Html.TextBoxFor(m =&gt; m.ServiceQaVisits[a].sqvComment)
                          @Html.ValidationMessageFor(m =&gt; m.ServiceQaVisits[a].sqvComment,"", new {@class = "text-danger"});
                      }
  
  
                  &lt;/div&gt;
              }
  
          }
  </code></pre>
  
  <p><strong>Model</strong></p>
  
  <pre><code>[Required(ErrorMessage = "Please enter the reason")]
      public string sqvComment { get; set; }
  </code></pre>
  
  <p>Any help is highly appreciated.</p>