c# - ASP.NET MVC 3 Cascading combobox doesn't work -


i have 2 comboboxes. first comobobox populate in such way , works fine:

@html.dropdownlistfor( x => x.town,  new selectlist(model.towns, "value", "text"), "-- select town --")      public ienumerable<selectlistitem> towns     {                 {             list<datarow> townslistdb = oracleselect("select * towns");             list<selectlistitem> townsitems = new list<selectlistitem>();              foreach (datarow rw in townslistdb)             {                 townsitems.add(new selectlistitem { value = rw[0].tostring(),                  text = rw[1].tostring() });             }              return townsitems;         }     } 

and depends on town, want show list of hospitals:

@html.dropdownlistfor( x => x.hospital,  enumerable.empty<selectlistitem>(), "-- select hospital --") 

my jquery code is:

 $('#town').change(function() {     var selectedtown = $(this).val();     if (selectedtown != null && selectedtown != '') {         $.getjson('@url.action("hospitals")', { town: selectedtown },          function           (hospitals) {             var hospitalsselect = $('#hospital');             hospitalsselect.empty();             $.each(hospitals, function(i, hospital) {                 hospitalsselect.append($('<option/>', {                         value: hospital.value,                         text: hospital.text                     }));             });             });      }  }); 

and c#:

        public actionresult hospitals(string town)         {             var modelhospital = new medicalviewmodel();             list<datarow> hospitalslistdb = modelhospital.oracleselect             ("select * hospitals hh hh.townid = " + town);             list<selectlistitem> hospitalsitems = new list<selectlistitem>();              foreach (datarow rw in hospitalslistdb)             { //example: //rw[0]=101111 //rw[1]=dublin                 hospitalsitems.add(new selectlistitem { value = rw[0].tostring(),                  text = rw[1].tostring() });             }             return json(                 hospitalsitems,                 jsonrequestbehavior.allowget);             return json(hospitalsitems, jsonrequestbehavior.allowget);         } 

but doesn't work. if use code return result, it's ok:

  return json(enumerable.range(1, 6).select(x => new { value = x, text = x }),                     jsonrequestbehavior.allowget                 ); 

why combobox doesn't work list result db?

use code :

return json(hospitalsitems.tolist(), jsonrequestbehavior.allowget); 

instead of in last line

return json(hospitalsitems, jsonrequestbehavior.allowget); 

Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -