在尝试更多关于级联下拉之后,我决定通过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。但没有工作。
答案 0 :(得分:4)
像这样:
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
});
}
}
OR,而不是使用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>");
});
});
});
});
像这样......