在尝试了更多关于级联下拉之后,我决定由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
<%= Html.DropDownList("CountryID") %> <select name="StateID" id="StateID"></select>
那么什么是getJSON参数?
我指的是blog.但没有工作.
解决方法
喜欢这个:
function bindOptions() { ddlStateID.options.length = 0; var CountryID = ddlCountry.value; if (CountryID) { var url = "/<YOUR CONTROLLER NAME>/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 = "/<YOUR CONTROLLER NAME>/States/" + strCountryIDs; $.getJSON(url,null,function(data) { $("#StateID").empty(); $.each(data,function(index,optionData) { $("#StateID").append("<option value='" + optionData.StateID + "'>" + optionData.StateName + "</option>"); }); }); }); });
这样的东西……