MVC4 Cascading DropDownList - reference selected value
-
I have the following code in my MVC application to set up Cascading Drop Down Lists:
public ActionResult Index(){
ViewBag.Clubs = db.Clubs.OrderByDescending(c => c.Name).ToList();
ViewBag.Comps = db.Competitions.OrderByDescending(c => c.StartDate).ToList();
ViewBag.Teams = db.Teams.OrderBy(t => t.Name).ToList();
return View();
}public IList GetCompetitionList(int clubid)
{...}public IList GetTeamList(int compid)
{...}[AcceptVerbs(HttpVerbs.Get)]
public JsonResult LoadCompsByClubId(string clubid)
{
var compsList = this.GetCompetitionList(Convert.ToInt32(clubid));
var compsData = compsList.Select(m => new SelectListItem(){
Text = m.Name,
Value = m.Id.ToString()
});
return Json(compsData, JsonRequestBehavior.AllowGet);
}[AcceptVerbs(HttpVerbs.Get)]
public JsonResult LoadTeamsByCompId(string compid)
{
var teamsList = this.GetTeamList(Convert.ToInt32(compid));
var teamsData = teamsList.Select(c => new SelectListItem(){
Text = c.Division.Name,
Value = c.Id.ToString()
});
return Json(teamsData, JsonRequestBehavior.AllowGet);
}public ActionResult PlayerList(Team team)
{
if (team == null)
{
return HttpNotFound();
}
var players = from p in db.Sharks_Player
join t in db.Sharks_Player_CompTeam
on p.Id equals t.PlayerId
orderby p.FirstName
where t.CompTeam_Id == team.Id
select p;
return View(players);
}And this is the .cshtml: