1

I'm trying to do the following: filter information from a listbox locations depending on what the user enters in a dropdownlist of cities. I followed step by step this site below (but in my case its just one dropdown and one listbox, not three):

Simple Implementation of Cascading Dropbox

My code:

  • I created an actionResult in my controller.

      [HttpPost]
        public ActionResult SelectMunicipio(int? selectedMUNICIPIO, SINCO_LOCALIDADE_CONCESSAO sinco_localidade_concessao, SINCO_CONCESSAO sinco_concessao)
        {
    
            ViewBag.IDMUNICIPIO = new SelectList(db.MUNICIPIOS_VIEW, "ID_MUNICIPIO", "NOME_MUNICIPIO", sinco_concessao.IDMUNICIPIO);
    
            ViewBag.IDLOCALIDADE = new SelectList(db.LOCALIDADES_VIEW, "ID_LOCALIDADE", "NOME_LOCALIDADE", sinco_localidade_concessao.IDLOCALIDADE);
    
            if (selectedMUNICIPIO.HasValue)
            {
                ViewBag.IDLOCALIDADE = (from s in db.LOCALIDADES_VIEW
                                        where s.ID_MUNICIPIO == selectedMUNICIPIO
                                        orderby s.NOME_LOCALIDADE
                                        select s);
    
            }
            return PartialView("LocalidadesList", sinco_concessao);
        }
    
  • I created 2 partial views: MunicipiosList and LocalidadesList.

    <b><script type="text/javascript"> $(function () {$("#selectedMUNICIPIO").change(function(){ $(this).parents('form').submit(); });});
    

    <b><div class="editor-label">
    <%: Html.LabelFor(model => model.IDMUNICIPIO) %>
    </div>
    <div class="editor-field">
    <% using (Ajax.BeginForm("SelectMunicipio", "ConcessaoController", new AjaxOptions { UpdateTargetId = "Localidades"})) { %><%: Html.DropDownList("IDMUNICIPIO", String.Empty) %></div><b>
    

--and--

   <b><div class="editor-label" style="font-weight: bold">
       Localidades:
    </div>

     <div class="editor-field">

            <%: Html.ListBox("IDLOCALIDADE", null, new { id = "IDLOCALIDADE", size = "10" })%></div><b>
  • Calling partial view.

          <%: Html.Partial("MunicipioList", Model)%>
    
        </div>
    

          <%: Html.Partial("LocalidadesList", Model)%>
    
        </div><b>
    
  • Add jquery-1.5.1.min.js and jquery.unobtrusive-ajax.min.js to the index.

  • Added to the web.config:

    <appSettings>
      <add key="webpages:Enabled" value="false" />
      <add key="ClientValidationEnabled" value="true"/>
      <add key="UnobtrusiveJavaScriptEnabled" value="true"/>
    </appSettings>
    

Sorry for the wrong format here, I never used stackoverflow.

What is happening is that my code is not identifying when the index of dropdownlist is changed, and because of it it does nothing. Does anyone know what could be wrong?

fejese
  • 4,601
  • 4
  • 29
  • 36
Jessica
  • 97
  • 1
  • 12

1 Answers1

0

using partial views for this is probably not the best idea, you could do it with jQuery as shown here:

How to populate a cascading Dropdown with JQuery

or use library like here:

http://demo.aspnetawesome.com/AjaxDropdownDemo

Community
  • 1
  • 1
Omu
  • 69,856
  • 92
  • 277
  • 407