title: Refreshing @Html.ListBox content with results from ajax call in MVC
body: <p>I have two <code>@Html.ListBox</code> in my view:</p>
  
  <pre><code>IEnumerable&lt;Models.CategoryModel&gt; categories = ViewBag.AllCategories;
  IEnumerable&lt;Models.TypeModel&gt; types = ViewBag.AllTypes;
  
  @Html.Label("Category", new { @class = "control-label col-md-2" })
  &lt;div class="col-md-10"&gt;
      @Html.ListBox("CategoriesFilter", new MultiSelectList(categories, "CatID", "Category"), new { @class = "chosen-select" })
  &lt;/div&gt;
  
   @Html.Label("Type", new { @class = "control-label col-md-2" })
   &lt;div class="col-md-10"&gt;
       @Html.ListBox("TypesFilter", new MultiSelectList(types, "TID", "Description"), new { @class = "chosen-select" })
   &lt;/div&gt;
  </code></pre>
  
  <p>When the user selects/deselects a category, I need to refresh the types in <code>@Html.ListBox("TypesFilter")</code>. I am doing an ajax call as follows:</p>
  
  <pre><code>$("#CategoriesFilter").change(function () {
      setTypesByCategory();
  });
  
  function setTypesByCategory() {
      var categoryIDs = ($('#CategoriesFilter').val());
  
      $.ajax({
          url: "@(Url.Action("SetTypesByCategory", "Issues"))",
          type: "POST",
          contentType: 'application/json; charset=utf-8',
          data: '{ "categoryIDs" : "' + categoryIDs + '" }',
          success: function (data) {
              $.each(data, function () {
                  $.each(this, function (k, v) {
                      window.alert("success !!!" + k + " test " + this.STID);
                  });
              });
  
              $("#TypesFilter").empty();
              $("#TypesFilter").val(data.TypesData);
          }              
      });
  }
  </code></pre>
  
  <p>And in my controller:</p>
  
  <pre><code>[System.Web.Mvc.HttpPost]
  public JsonResult SetTypesByCategory(string categoryIDs)
  {
      string[] categories = categoryIDs.Split(',');
      List&lt;TypeModel&gt; filterdTypesList = new List&lt;TypeModel&gt;();
      foreach (var catID in categories)
      {
          foreach (var type in types.Where(m =&gt; m.CatID == Int32.Parse(catID)))
          {
              filterdTypesList.Add(type);
          }
      }
      int modelCount = filterdTypesList.Count();
      IEnumerable&lt;TypeModel&gt; filterdTypes = filterdTypesList;
  
      return Json(new { TypesData = filterdTypes, ModelCount = modelCount });
  }
  </code></pre>
  
  <p>First, the types list box is not being empty when using <code>$("#TypesFilter").empty();</code>. And how can i populate this listbox with the obtained results?</p>