我在这里遇到问题。我希望用户从下拉列表中选择一个团队并让它通过jQuery或其他方法执行一个方法,然后填充列表框。我希望它只显示该球队的球员,以便我可以与其他球队进行交易。
我的设置: Trade Players View
目前,我已经制作了它,它显示了ListBox中的所有玩家。 在用户选择团队之前,我需要它才能显示任何内容。
TradePlayersView的代码:
@model Baseball.Models.ViewModels.TradeVM
@{
ViewBag.Title = "TradePlayers";
}
<h2 align="center">Trade Players</h2>
<div class="col-xs-12" align="center">
<div class="row">
@using (Html.BeginForm())
{
<div class="col-sm-6 col-lg-offset-3">
<div class="panel panel-primary">
<div class="panel-heading">
<h4 class="panel-title">Select Teams</h4>
</div>
<div class="panel-body">
<div class="form-group">
<label>Trade Player from Team:</label>
@Html.DropDownListFor(m => m.Team1.Id, Model.Team1Items, "-Choose Team-", new { @class = "form-control" })
@Html.ListBoxFor(m => m.Players1, Model.Players1, new { @class = "form-control" })
</div>
<div class="form-group">
<label>To Team:</label>
@Html.DropDownListFor(m => m.Team2.Id, Model.Team2Items, "-Choose Team-", new { @class = "form-control" })
@Html.ListBoxFor(m => m.Players1, Model.Players1, new { @class = "form-control" })
</div>
<button class="btn btn-sm btn-success">Trade</button>
@Html.ActionLink("Cancel", "Index", "Home", null, new { @class = "btn btn-sm btn-danger" })
</div>
</div>
</div>
}
</div>
</div>
<br/>
TradeVM代码:
public class TradeVM
{
public Team Team1 { get; set; }
public Team Team2 { get; set; }
public List<SelectListItem> Players1 { get; set; }
public List<SelectListItem> Players2 { get; set; }
public List<SelectListItem> Team1Items { get; set; }
public List<SelectListItem> Team2Items { get; set; }
public TradeVM()
{
Players1 = new List<SelectListItem>();
Players2 = new List<SelectListItem>();
Team1 = new Team();
Team2 = new Team();
Team1Items = new List<SelectListItem>();
Team2Items = new List<SelectListItem>();
}
public void SetPlayer1Items(IEnumerable<Player> players)
{
foreach (var player in players)
{
Players1.Add(new SelectListItem()
{
Value = player.Id.ToString(),
Text = player.LastName
});
}
}
public void SetPlayer2Items(IEnumerable<Player> players)
{
foreach (var player in players)
{
Players2.Add(new SelectListItem()
{
Value = player.Id.ToString(),
Text = player.LastName
});
}
}
public void SetTeam1Items(IEnumerable<Team> teams)
{
foreach (var team in teams)
{
Team1Items.Add(new SelectListItem()
{
Value = team.Id.ToString(),
Text = team.Name
});
}
}
public void SetTeam2Items(IEnumerable<Team> teams)
{
foreach (var team in teams)
{
Team2Items.Add(new SelectListItem()
{
Value = team.Id.ToString(),
Text = team.Name
});
}
}
}
我无法找到一个好的答案..我很感激任何答案。
编辑: 我可能应该将我的代码包含在控制器中。
[HttpGet]
public ActionResult TradePlayers()
{
var trade = new TradeVM();
var manager = new TeamManager();
var playerManager = new PlayerManager();
//var team1 = manager.GetTeamById(trade.Team1.Id);
//var team2 = manager.GetTeamById(trade.Team2.Id);
//var player1 = team1.Players;
//var player2 = team2.Players;
trade.SetTeam1Items(manager.GetAllTeams());
trade.SetTeam2Items(manager.GetAllTeams());
trade.SetPlayer1Items(playerManager.GetAllPlayers());
trade.SetPlayer2Items(playerManager.GetAllPlayers());
// Need to specify players for that team.
return View(trade);
}
//Work in progress
//[HttpPost]
//public ActionResult TradePlayers(TradeVM trade)
//{
// var manager = new TeamManager();
// manager.TradePlayers(trade.Team1, trade.Team2, trade.Team1.Player, trade.Team1.Player);
// return RedirectToAction("Teams");
//}