views:

100

answers:

1

Hi, I' still newby to this so I'll try to explain what I'm doing. Basically what I want is to load a dropdownlist depending on the value of a previous one, and I want it to load the data and appear when the other one is changed. This is the code I've written in my controller:

public ActionResult GetClassesSearch(bool ajax, string phylumID, string kingdom){
  IList<TaxClass> lists = null;
  int _phylumID = int.Parse(phylumID);
  int _kingdom = int.Parse(kingdom);
  lists = _taxon.getClassByPhylumSearch(_phylumID, _kingdom);
  return Json(lists.count);
}

and this is how I call the method from the javascript function:

function loadClasses(_phylum) {
 var phylum = _phylum.value;

 $.getJSON("/Suspension/GetClassesSearch/",
         { ajax: true,
             phylumID: phylum,
             kingdom: kingdom
         },
         function(data) {
             alert(data);
             alert('no fallo')
             document.getElementById("pClass").style.display = "block";
             document.getElementById("sClass").options[0] = new Option("-select-", "0", true, true);
             //for (i = 0; i < data.length; i++) {
             //   $('#sClass').addOption(data[i].classID, data[i].className);
             //}
         });

}

The HTML associated is:

<p id="pPhylum">
                <%= Html.Label("Phylum: ") %>
                <%= Html.DropDownList("sPhylum",
                          (SelectList)ViewData["PhyRecID"],
                      "--Select One--",
                      new { onchange = "loadClasses(this);" }
                      )%>
</p>
<p id="pClass">
                <%= Html.Label("Class: ") %> <select id="sClass"></select>
</p>

The thing is that just like this it works, I pass the function the number of classes within a selected phylum, and it displays the pclass element, the problem gets when I try to populate the slist with data (which should contain the objects retrieved from the database), because when there is data returned by the database changing return Json(lists) instead of return Json(lists.count) I keep getting the same error:

A circular reference was detected while serializing an object of type 'SubSonic.Schema.DatabaseColumn'.

I've been going round and round debugging and making tests but I can't make it work, and it is suppossed to be a simple thing, but I'm missing something. I have commented the for loop because I'm not quite sure if that's the way you access the data, because I've not been able to make it work when it finds records. Can anyone help me?

Thanks in advance, Victor

A: 

Hi,

Finally I solved this problem. The problem was that JSON for some reason wasn't capable of displaying objects that are referenced to each other, that have problems with foreign keys and stuff. I created a new class just with the fields I needed from the database, and I populated it with a foreach statement. Then I passed that filled IList to the return Json and it worked...

Thank you all for your help, Victor

vikitor