如何让Json对象下拉?

在尝试了更多关于级联下拉之后,我决定由Jquery来做。

这是在我的cityController中

public ActionResult States(int id) { AcademicERP.Models.AcademicERPDataContext dc = new AcademicERPDataContext(); var states = from s in dc.States where s.CountryID == id select s; return Json(states.ToList()); } 

我试图从中调用它

城市/创建有脚本的页面

  var ddlCountry; var ddlStateID; function pageLoad() { ddlStateID = $get("StateID"); ddlCountry = $get("CountryID"); $addHandler(ddlCountry, "change", bindOptions); bindOptions(); } function bindOptions() { ddlStateID.options.length = 0; var CountryID = ddlCountry.value; if (CountryID) { // some logic to call $.getJSON() } 

我在视图中有DD

   

那么什么是getJSON参数? 我指的是博客 。 但没有工作。

像这样:

 function bindOptions() { ddlStateID.options.length = 0; var CountryID = ddlCountry.value; if (CountryID) { var url = "//States/" + CountryID; $.get(url, function(data) { // do you code to bind the result back to your drop down }); } } 

或者,而不是使用pageLoad,我将纯粹由jQuery使用它:

 $(document).ready(function() { $("#CountryID").change(function() { var strCountryIDs = ""; $("#CountryID option:selected").each(function() { strCountryIDs += $(this)[0].value; }); var url = "//States/" + strCountryIDs; $.getJSON(url, null, function(data) { $("#StateID").empty(); $.each(data, function(index, optionData) { $("#StateID").append(""); }); }); }); }); 

这样的东西……