我有一个页面,其中有LeagueDDL
,ClubDDL
,然后是玩家列表。我想要实现的是用户将选择联盟,之后俱乐部根据联盟的ID被绑定到第二个DDL,并且当用户也选择俱乐部时,玩家列表将被绑定到视图取决于ClubID。
到目前为止,我有以下内容: -
查看: -
<script type="text/javascript">
function populateClubsDropdown() {
$("#ddlClubs").empty();
var typeID = getLeagueID();
$.getJSON('@Url.Action("GetClubsByLeague", "Footballer")?TypeID=' + typeID, function (result) {
$.each(result, function () {
$("<option>").attr("value", this.ClubID).text(this.Club).appendTo("#ddlClubs");
});
});
}
function getLeagueID() {
var leagueID = $("#ddlLeagues").val();
return leagueID;
}
</script>
@Html.DropDownListFor(model => model.League.LeagueID, Model.LeagueList,
new { id = "ddlLeagues", onChange = "$:populateClubsDropdown()" })
@Html.DropDownListFor(model => model.Club.ClubID, Model.ClubList, new { id = "ddlClubs" })
在我的控制器里我有: -
public ViewResult Index()
{
League league = new League();
string selected = "Please Select";
model.LeagueList = model.PopulateLeagueDDL(selected, league, null);
Club club = new Club();
model.ClubList = model.PopulateClubDDL(selected, league, club, null);
model.Footballers = db.Footballers.Include("Club").Include("Position").Include("Status").Include("Country");
return View(model);
}
public JsonResult GetClubsByLeague(int LeagueID)
{
var result = footballersDAL.GetClubsByLeagueId(LeagueID).Select(
c => new { ClubID = c.ClubID, Club = c.ClubName });
JsonResult jsonResult = new JsonResult { Data = result, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
return jsonResult;
}
当我第一次启动页面时,两个下拉列表都是正确的,但是当我更改LeagueDDL
时,我得到一个空的第二个DDL。并且Json断点没有被击中。
我希望得到一些帮助,我无法弄清楚我的错误。
更新视图
<script type="text/javascript">
function populateClubsDropdown() {
$("#ddlClubs").empty();
var typeID = getLeagueID();
$.getJSON('@Url.Action("GetClubsByLeague", "Footballer")?TypeID=' + typeID, function (result) {
$.each(result, function () {
$("<option>").attr("value", this.ClubID).text(this.Club).appendTo("#ddlClubs");
});
});
}
function getLeagueID() {
var leagueID = $("#ddlLeagues").val();
return leagueID;
$(function () {
$('#ddlLeagues').change(populateClubsDropdown);
});
}
@Html.DropDownListFor(model => model.League.LeagueID, Model.LeagueList,
new { id = "ddlLeagues" })
@Html.DropDownListFor(model => model.Club.ClubID, Model.ClubList, new { id = "ddlClubs" })
答案 0 :(得分:2)
看起来问题就在这里:
@Html.DropDownListFor(model => model.League.LeagueID, Model.LeagueList,
new { id = "ddlLeagues", onChange="$:populateClubsDropdown()" }) //What is"$:"?!
更改为:
@Html.DropDownListFor(model => model.League.LeagueID, Model.LeagueList,
new { id = "ddlLeagues", onChange = "populateClubsDropdown()" })
jQuery unobtrusive事件监听器的使用是更好的事件:
<script>
function populateClubsDropdown() {
$("#ddlClubs").empty();
var typeID = getLeagueID();
$.getJSON('@Url.Action("GetClubsByLeague", "Footballer")?TypeID=' + typeID, function (result) {
$.each(result, function () {
$("<option>").attr("value", this.ClubID).text(this.Club).appendTo("#ddlClubs");
});
});
}
$(function(){
$('#ddlLeagues').change(populateClubsDropdown);
});
</script>
@Html.DropDownListFor(model => model.League.LeagueID, Model.LeagueList,
new { id = "ddlLeagues"})
@Html.DropDownListFor(model => model.Club.ClubID, Model.ClubList,
new { id = "ddlClubs" })
<强>更新强>
看起来你向行动发送了错误的参数:
应为LeagueID
,但您发送TypeId
〜