title: Adding MVC DropdownList HTML Helper Dynamically using jQuery
body: <p>I am trying to add DropDownList helper dynamically using jQuery</p>
  
  <p>This code i wrote successfully create the dropdown lists as needed, however code render the  HTML tags in unexpected format, where it places double quotes for each attributes' value!! (e.g name=""HousingUnitCount"") </p>
  
  <pre><code>&lt;select class="&amp;quot;form-control" input-sm&amp;quot;="" id="&amp;quot;HousingUnitCount&amp;quot;" name="&amp;quot;HousingUnitCount&amp;quot;"&gt;&lt;option value="&amp;quot;&amp;quot;"&gt;Choose the number of housing Unit&lt;/option&gt;
  &lt;option value="&amp;quot;0&amp;quot;"&gt;0&lt;/option&gt;
  &lt;option value="&amp;quot;1&amp;quot;"&gt;1&lt;/option&gt;
  &lt;option value="&amp;quot;2&amp;quot;"&gt;2&lt;/option&gt;
  &lt;option value="&amp;quot;3&amp;quot;"&gt;3&lt;/option&gt;
  &lt;option value="&amp;quot;4&amp;quot;"&gt;4&lt;/option&gt;
  &lt;option value="&amp;quot;5&amp;quot;"&gt;5&lt;/option&gt;
  &lt;/select&gt;
  </code></pre>
  
  <p>Below is the code i have,</p>
  
  <p>Here I am defining global variables for my list and HTML Helper to be used in jQuery</p>
  
  <pre><code>@{
  ViewBag.Title = "TestingDynamicFormWithjQuery";
  
  var unitsCountList = new List&lt;SelectListItem&gt; { new SelectListItem() {Text="0", Value="0"},
                                                  new SelectListItem() {Text="1", Value="1"},    
                                                  new SelectListItem() {Text="2", Value="2"},
                                                  new SelectListItem() {Text="3", Value="3"},
                                                  new SelectListItem() {Text="4", Value="4"},
                                                  new SelectListItem() {Text="5", Value="5"}
                                                };
  
  var housingUnit = Html.DropDownList("HousingUnitCount", unitsCountList, "Choose the number of housing Unit", htmlAttributes: new { @class = "form-control input-sm" }).ToHtmlString();
  }
  </code></pre>
  
  <p>This is the HTML i am using</p>
  
  <pre><code>&lt;button id="addUnitBtn" type="button" name="addUnitBtn" class="btn btn-primary"&gt;
      &lt;span class="glyphicon glyphicon-plus-sign"&gt;&lt;/span&gt; Add Unit
  &lt;/button&gt;
  
  @using(Html.BeginForm())
  {
  
      &lt;div id="addUnitDiv"&gt;
  
      &lt;/div&gt;
  
      &lt;br/&gt;&lt;br /&gt;&lt;br /&gt;
      &lt;input id="Submit" type="submit" value="Save" class="btn btn-primary btn-lg" /&gt;
  }
  </code></pre>
  
  <p>Below is the jQuery code to add DropDownList dynamically</p>
  
  <pre><code>@section Scripts{
  &lt;script type="text/javascript"&gt;
      $(window).ready(function () {
          console.log('windows ready');
          $('button[name="addUnitBtn"]').on("click", function () {
              console.log('btn clicked');
              $("#addUnitDiv").append('&lt;div&gt;@Ajax.JavaScriptStringEncode(housingUnit)&lt;/div&gt;');
          });
      });
  &lt;/script&gt;
  }
  </code></pre>