Currently, I am developing a form that allows users to select up to 11 football players from a list using checkboxes. The players are categorized and named by their playing positions.
@if(item.PlayerPosition.FantasyFootball_PlayerToSeason.Select(x => x.PositionID == 1).FirstOrDefault()) {
@Html.CheckBox("goalkeepers", false)
}
@if(item.PlayerPosition.FantasyFootball_PlayerToSeason.Select(x => x.PositionID == 2).FirstOrDefault()) {
@Html.CheckBox("defenders", false)
}
@if(item.PlayerPosition.FantasyFootball_PlayerToSeason.Select(x => x.PositionID == 3).FirstOrDefault()) {
@Html.CheckBox("midfielders", false)
}
@if(item.PlayerPosition.FantasyFootball_PlayerToSeason.Select(x => x.PositionID == 4).FirstOrDefault()) {
@Html.CheckBox("forwards", false)
}
To display the selected values in a pop-up box when a button is clicked, I have utilized the following JavaScript code snippet:
$('#button').click(function() {
alert($('input[type="checkbox"]:checked').eq(0).val()); // 1st
alert($('input[type="checkbox"]:checked').eq(1).val()); // 2nd
alert($('input[type="checkbox"]:checked').eq(2).val()); // 3rd
alert($('input[type="checkbox"]:checked').eq(3).val()); // 4th
alert($('input[type="checkbox"]:checked').eq(4).val()); // 5th
alert($('input[type="checkbox"]:checked').eq(5).val()); // 6th
alert($('input[type="checkbox"]:checked').eq(6).val()); // 7th
alert($('input[type="checkbox"]:checked').eq(7).val()); // 8th
alert($('input[type="checkbox"]:checked').eq(8).val()); // 9th
alert($('input[type="checkbox"]:checked').eq(9).val()); // 10th
alert($('input[type="checkbox"]:checked').eq(10).val()); // 11th
});
My current challenge lies in passing these selected values to the controller so that they can be stored in the database. Since these checkboxes are part of a partial view and not directly related to the model, this task is proving to be complex.
I am open to suggestions on how to efficiently capture the IDs of each selected player for recording purposes. It would be ideal to record each player's ID individually rather than as an array.
In the excerpt below from my controller, I have outlined where I intend to include the selected values in my table (currently commented out):
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "FantasyTeamID,Id,FantasyTeamTypeID,Player1,Player2,Player3,Player4,Player5,Player6,Player7,Player8,Player9,Player10,Player11,FirstEntered,LastUpdated,FormationID,GameweekID")] FantasyFootball_FantasyTeam fantasyfootball_fantasyteam)
{
if (ModelState.IsValid)
{
db.FantasyFootball_FantasyTeam.Add(fantasyfootball_fantasyteam);
fantasyfootball_fantasyteam.Id = User.Identity.GetUserId();
fantasyfootball_fantasyteam.FantasyTeamTypeID = 1;
//fantasyfootball_fantasyteam.Player1 = 2398;
//fantasyfootball_fantasyteam.Player2 = 491;
//fantasyfootball_fantasyteam.Player3 = 850;
//fantasyfootball_fantasyteam.Player4 = 461;
//fantasyfootball_fantasyteam.Player5 = 2845;
//fantasyfootball_fantasyteam.Player6 = 482;
//fantasyfootball_fantasyteam.Player7 = 1028;
//fantasyfootball_fantasyteam.Player8 = 2516;
//fantasyfootball_fantasyteam.Player9 = 2586;
//fantasyfootball_fantasyteam.Player10 = 2230;
//fantasyfootball_fantasyteam.Player11 = 2893;
fantasyfootball_fantasyteam.FirstEntered = DateTime.Now;
fantasyfootball_fantasyteam.FormationID = 1;
fantasyfootball_fantasyteam.GameweekID = 47;
db.SaveChanges();
return RedirectToAction("Index");
}