title: cascading dropdowns problem using ajax on asp.net mvc3
body: <p>cascading dropdowns problem using ajax on asp.net mvc3</p>
  
  <p>this is the code in the controller</p>
  
  <pre><code>[HttpPost]
  public ActionResult subcats(int categoriaId)
  {
      var subcategorias = anunciorepo.GetSubCategoriaByCat_id(categoriaId).ToList();
  
      return Json(subcategorias, JsonRequestBehavior.AllowGet);
  }
  </code></pre>
  
  <p>this is the view</p>
  
  <pre><code>@model agro.ViewModel.AnunciosViewModel
  @{
      ViewBag.Title = "Create";
  }
  
  &lt;h2&gt;Create&lt;/h2&gt;
  
  &lt;script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"&gt;&lt;/script&gt;
  &lt;script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"&gt;&lt;/script&gt;
  &lt;script src="@Url.Content("~/Scripts/MicrosoftAjax.js")" type="text/javascript"&gt;&lt;/script&gt;
  &lt;script src="@Url.Content("~/Scripts/MicrosoftMvcAjax.js")" type="text/javascript"&gt;&lt;/script&gt;
  
  &lt;script type="text/javascript"&gt;
      $(function () {
          var urlemp = '@Url.Action("subcats")';
          $("#categorias").change(function () {
              window.alert("cat :" + $(this).val());
              var categoriaId = $(this).val();
              window.alert(categoriaId);
              $.ajax({ 
                  type: 'POST',
                  url: urlemp,
                  data: { categoriaId: categoriaId },
                  datatype:'JSON',
                  // contentType: "application/json; charset=utf-8",
  
                  success: function (response) {
                      window.alert(categoriaId);
                      window.alert("sub");
                      $("#subcategorias").fillSelect(response);
                  },
  
                  error: function (xhr, type, exception) {
                      window.alert(categoriaId);
  
                      alert("Error: " + type);
                      alert("Error: " + exception);
                      alert("Error: " + xhr.status);
                   }
              })
          });
      });
  &lt;/script&gt; 
  
  @using (Html.BeginForm("Create", "Anuncios", FormMethod.Post, new { enctype = "multipart/form-data" }))
  {
      @Html.ValidationSummary(true)
  
     &lt;fieldset&gt;
  
     &lt;div class="editor-field"&gt;&lt;/div&gt;
     &lt;div class="editor-label"&gt;
         Categorias
     &lt;/div&gt;
     &lt;div class="editor-field"&gt;
         @Html.DropDownList("categorias", (SelectList)ViewData["Categorias"], "----Selecciona um----"
        )
         @*Html.DropDownList("language", (SelectList)ViewData["Categorias"], "----Selecciona um----", new { onchange = "javascript:cascadingdropdown();" }) *@
         @Html.ValidationMessageFor(model =&gt; model.anuncio.codigo_categoria)
     &lt;/div&gt;
     &lt;div class="editor-label"&gt;
         Sub Categorias
     &lt;/div&gt;
     &lt;div class="editor-field"&gt;
         &lt;select id="subcategorias" name="subcategorias"&gt;&lt;/select&gt;
         @* @Html.DropDownList("languagesub", Enumerable.Empty&lt;SelectListItem&gt;(),"--- Selecciona um---")*@                  
         @Html.ValidationMessageFor(model =&gt; model.anuncio.codigo_subcategoria)
     &lt;/div&gt;
     &lt;div class="editor-label"&gt;
         @Html.LabelFor(model =&gt; model.anuncio.titulo)
     &lt;/div&gt;
     &lt;div class="editor-field"&gt;
         @Html.EditorFor(model =&gt; model.anuncio.titulo)
         @Html.ValidationMessageFor(model =&gt; model.anuncio.titulo)
     &lt;/div&gt;
     &lt;div class="editor-label"&gt;
         @Html.LabelFor(model =&gt; model.anuncio.decricao)
     &lt;/div&gt;
     &lt;div class="editor-field"&gt;
         @Html.EditorFor(model =&gt; model.anuncio.decricao)
         @Html.ValidationMessageFor(model =&gt; model.anuncio.decricao)
     &lt;/div&gt;
     &lt;div class="editor-label"&gt;
         Avatar
     &lt;/div&gt;
     &lt;div class="editor-field"&gt;
         &lt;input type="file" name="Avatar" /&gt;
     &lt;/div&gt;
     &lt;p&gt;
         &lt;input type="submit" value="Create" /&gt;
     &lt;/p&gt;
  
     &lt;/fieldset&gt;
  
  }
  
  &lt;div&gt;
      @Html.ActionLink("Back to List", "Index")
  &lt;/div&gt;
  </code></pre>
  
  <p>always goes to error </p>
  
  <pre><code>exception:undifined
  xhr.status=500
  alert("Error: " + type);
  alert("Error: " + exception);
  alert("Error: " + xhr.status
  </code></pre>
  
  <p>thanks
  Pedro</p>