title: Bind Dropdownlist in Partial View
body: <p>I need to create a View where I can add additional forms for user input.  The form has several dropdownlists.  I'm trying to add another form when the user clicks "Add New" button, but I am unable to figure out how to do it as well as how to bind the dropdownlists in the partial view.</p>
  
  <p>Below is the code in the main View:</p>
  
  <pre><code>  @using (Html.BeginForm())
      {
          &lt;form class="autoWidth"&gt;
              &lt;h4 class="hr"&gt;
                  &lt;a class="btn btn-danger icon-subtract sm btn-xs" data-nodrag ng-click="remove(this)"&gt;&lt;/a&gt;
              &lt;/h4&gt;
              &lt;div class=""&gt;
                  &lt;span class="inline-block"&gt;
                      @Html.LabelFor(model =&gt; model.LoanClasses, new { @class = "control-label" })
                      @Html.DropDownListFor(model =&gt; model.SelectedLoanClass, Model.LoanClasses, new { @class = "inputROW" })
                  &lt;/span&gt;   
                  &lt;span class="inline-block"&gt;
                      @Html.LabelFor(model =&gt; model.LoanPurpose, new { @class="control-label"})
                      @Html.DropDownListFor(model =&gt; model.SelectedLoanPurpose, Model.LoanPurpose, new { @class = "inputROW" })
  
                  &lt;/span&gt;
                  &lt;span class="inline-block"&gt;
                      @Html.LabelFor(model =&gt; model.AmountRequested, new { @class = "control-label" })
                      @Html.TextBoxFor(model =&gt; model.AmountRequested, new{@class="inputROW"})
                  &lt;/span&gt;  
                  &lt;span class="inline-block"&gt;
                      @Html.LabelFor(model =&gt; model.RequestedRate, new { @class = "control-label" })
                      @Html.TextBoxFor(model =&gt; model.RequestedRate, new { @class = "inputROW" })
                  &lt;/span&gt; 
              &lt;/div&gt;
              &lt;div class="breakup"&gt;
                  &lt;span class="inline-block"&gt;
                      @Html.LabelFor(model =&gt; model.LoanTypes, new { @class = "control-label" })
                      @Html.DropDownListFor(model =&gt; model.SelectedLoanType, Model.LoanTypes, new { @class = "inputROW" })
                      &lt;span id="types-loading-progress" style="display: none;"&gt;Please wait...&lt;/span&gt;
                  &lt;/span&gt;
                  &lt;span class="inline-block"&gt;
                      @Html.LabelFor(model =&gt; model.RateIndex, new { @class = "control-label" })
                      @Html.DropDownListFor(model =&gt; model.SelectedRateIndex, Model.RateIndex, new { @class = "inputROW" })
                  &lt;/span&gt;
                  &lt;span class="inline-block"&gt;
                      @Html.LabelFor(model =&gt; model.OriginationFeePercentage, new { @class = "control-label" })
                      @Html.TextBoxFor(model =&gt; model.OriginationFeePercentage, new { @class = "inputROW" })
                  &lt;/span&gt;
              &lt;/div&gt;
              &lt;div class="breakup"&gt;
                  &lt;span class="inline-block"&gt;
                      @Html.LabelFor(model =&gt; model.VariableRateFloor, new { @class = "control-label" })
                      @Html.TextBoxFor(model =&gt; model.VariableRateFloor, new { @class = "inputROW" })
                  &lt;/span&gt;
                  &lt;span class="inline-block"&gt;
                      @Html.LabelFor(model =&gt; model.VariableRateCeiling, new { @class = "control-label" })
                      @Html.TextBoxFor(model =&gt; model.VariableRateCeiling, new { @class = "inputROW" })
                  &lt;/span&gt;
                  &lt;span class="inline-block"&gt;
                      @Html.LabelFor(model =&gt; model.VariableRateMargin, new { @class = "control-label" })
                      @Html.TextBoxFor(model =&gt; model.VariableRateMargin, new { @class = "inputROW" })
                      &lt;/span&gt;
               &lt;/div&gt;
              &lt;div class="breakup"&gt;
                  &lt;span class="inline-block"&gt;
                      @Html.LabelFor(model =&gt; model.PaymentType, new { @class = "control-label" })
                      @Html.DropDownListFor(model =&gt; model.SelectedPaymentType, Model.PaymentType)
                  &lt;/span&gt;
                  &lt;span class="inline-block"&gt;
                      @Html.LabelFor(model =&gt; model.PaymentPeriod, new { @class = "control-label" })
                      @Html.DropDownListFor(model =&gt; model.SelectedPaymentPeriod, Model.PaymentPeriod)
                  &lt;/span&gt;
                  &lt;span class="inline-block"&gt;
                      @Html.LabelFor(model =&gt; model.NumberOfPayments, new { @class = "control-label" })
                      @Html.TextBoxFor(model =&gt; model.NumberOfPayments, new { @class = "inputROW" })
                  &lt;/span&gt;
                  &lt;span class="inline-block"&gt;
                      @Html.LabelFor(model =&gt; model.AmortizationTerm, new { @class = "control-label" })
                      @Html.TextBoxFor(model =&gt; model.AmortizationTerm, new { @class = "inputROW" })
                  &lt;/span&gt;
              &lt;/div&gt;
  
          &lt;/form&gt;
      }
      &lt;div id="newrow"&gt;
      &lt;div&gt;&lt;input id="addBtn" type="button" value="Add New" /&gt;&lt;/div&gt;
  &lt;/div&gt;
  
  &lt;script&gt;
      $("#addBtn").on("click", function () {
  
          $.get('@Url.Action("AddNewRow")', function () {
              $('#newrow');
          });
  
      });
  &lt;/script&gt;
  &lt;script type="text/javascript"&gt;
      $(function () {
          $("#SelectedLoanClass").change(function () {
                   var selectedItem = $(this).val();
                   var ddlTypes = $("#SelectedLoanType");
                var typesProgress = $("#types-loading-progress");
               typesProgress.show();
               $.ajax({
                   cache: false,
                   type: "GET",
                   url: "@(Url.RouteUrl("GetLoanTypesByClass"))",
                      data: { "LoanClassId": selectedItem },
                      success: function (data) {
                          ddlTypes.html('');
                          $.each(data, function (id, option) {
                              ddlTypes.append($('&lt;option&gt;&lt;/option&gt;').val(option.id).html(option.name));
                          });
                          typesProgress.hide();
                      },
                      error: function (xhr, ajaxOptions, thrownError) {
                          alert('Failed to retrieve types.');
                          typesProgress.hide();
                      }
                  });
              });
          });
  &lt;/script&gt; 
  </code></pre>
  
  <p>The partial view has the exact same code in between @Html.BeginForm{} as the Main View (it is supposed to be an identical form).</p>
  
  <p>Below is the code from my controller.  the "New" method fills the Main View:</p>
  
  <pre><code>[HttpGet]
      [Route("New")]
      public async Task&lt;ActionResult&gt; New()
      {
  
          var loanClasses = await GetLoanClasses();
          var loanTypes = await GetLoanTypes();
          var loanPurposes = await GetLoanPurposes();
          var rateIndex = await GetRateIndex();
          var paymentPeriod = GetPaymentPeriodList();
          var paymentTypes = await GetPaymentTypes();
  
  
         var model = new LoanRequestRowViewModel();
  
          model.LoanClasses.Add(new SelectListItem {Text = "-Please Select-", Value ="-1"});
          foreach(var _class in loanClasses)
          {
              model.LoanClasses.Add(new SelectListItem()
                  {
                      Text = _class.Text.ToString(),
                      Value = _class.Value.ToString()
                  });
          }
  
          model.LoanTypes.Add(new SelectListItem { Text = "-Please Select-", Value = "-1" });
          foreach(var _type in loanTypes)
          {
              model.LoanTypes.Add(new SelectListItem()
                  {
                      Text = _type.Text.ToString(),
                      Value = _type.Value.ToString()
                  });
          }
  
          model.LoanPurpose.Add(new SelectListItem { Text = "-Please Select-", Value = "-1" });
          foreach(var purpose in loanPurposes)
          {
              model.LoanPurpose.Add(new SelectListItem()
                  {
                      Value = purpose.Value.ToString(),
                      Text = purpose.Text.ToString()
                  });
          }
  
          model.RateIndex.Add(new SelectListItem {Text ="-Please Select-", Value = "-1"});
          foreach (var index in rateIndex)
          {
              model.RateIndex.Add(new SelectListItem()
                  {
                      Value = index.Value.ToString(),
                      Text = index.Text.ToString()
                  });
  
          }
          foreach(var period in paymentPeriod)
          {
              model.PaymentPeriod.Add(new SelectListItem()
                  {
                      Value = period.Value.ToString(),
                      Text = period.Text.ToString()
                  });
          }
          model.PaymentType.Add(new SelectListItem{Text="-Please Select-", Value ="-1"});
          foreach(var paytype in paymentTypes)
          {
              model.PaymentType.Add(new SelectListItem()
                  {
                      Value = paytype.Value.ToString(),
                      Text = paytype.Text.ToString()
                  });
          }
  
  
  
          return View(model);
      }
  </code></pre>
  
  <p>I was trying to use the following to add the partial View.  The View did try to load (I had different code in the script, but it was still incorrect), but it kept throwing "object not set to an instance.." when it tried to render the dropdownlists on the partial.  Hence I have two problems, rendering the partial on click of Add Button, and binding the dropdownlists on the partial once it is rendered.</p>
  
  <pre><code>   [HttpGet]
          [Route("Create")]
          public ActionResult AddNewRow()
          {
  
              return PartialView("_newLoanRequest");
          }
  </code></pre>
  
  <p>Any assistance will be greatly appreciated!</p>
  
  <p>I realized that I was not explaining this well so here is what the result should look like:</p>
  
  <pre><code>Loan Request 1
  
  Loan Class     Loan Purpose    Requested Amount, etc. etc.
  
  Loan Type       Rate Index     Origination Fee, etc. etc.
  
  
  Loan Request 2
  
  Loan Class     Loan Purpose    Requested Amount, etc. etc.
  
  Loan Type      Rate Index      Origination Fee, etc. etc. 
  
  Loan Request 3
  
  Loan Class      Loan Purpose      Requested Amount, etc. etc.
  
  Loan Type       Rate Index        Origination Fee, etc. etc.
  </code></pre>
  
  <p>I was trying to use the partial view to keep rendering new blank loan requests.  They want each of them to show in a sort of list like above.  If there is a better way to achieve the desired results, any guidance is appreciated!</p>