I want to populate one drop down list based on the selection of another drop down list.
I know it’s a fairly common problem, and for two whole days I’ve been trying different samples that I find on the net, to no avail!
Here is my code:
controller:
public List<SelectListItem> listTestCases(string id)
{
IQueryable<SelectListItem> A = repository.listTestCases(id);
return (A.toList());
}
public JsonResult listTestCases_2(string id)
{
IQueryable<SelectListItem> A = repository.listTestCases(id);
return Json(A);
}
I have two methods, one of them returns a List, the other one JSONResults, and I tried everything I found on the net with both methods.
script:
<script>
$(document).ready(function() {
$('select#testCategoriesUniqueId').change(function() {
var testCategory = $(this).val();
$.ajaxSetup({ cache: false });
alert(testCategory);
$.getJSON("TestInput/listTestCases/" , {id: testCategory}, function(data) {
alert("here");
});
});
});
With the above code, the code goes through the listTestCases, and get’s the results, but It doesn’t go to the success function. (doesn’t alert)
I have tried:
$.getJSON("TestInput/listTestCases/" + testCategory, null, function(data) {
alert("here");
});
not surprisingly same results.
I even tried : (json-p)
$.ajax({
url: "TestInput/listTestCases/" + testCategory,
dataType: "json-p",
success: function(data) {
alert(data.length);
var S = "";
$("#testCasesUniqueId").removeOption(/./);
for (var i = 0; i < data.length; i++) {
S += data[i];
var val = data[i].Value;
var text = data[i].Text;
$("#testCasesUniqueId").addOption(val, text, false);
}
alert(S);
}
}
);
and the result is a string (System.collections.List[…SelectListItem]. and does not give me the objects of the list. so I tried it with listTestCases2, and it is not even calling the method.
I read somewhere that I need to use post instead of get, and I even tried postJSON. and no result.
Basically I’m super lost.
Somewhere I read that I need to use “post” method for my purpose.
Works for me.