asp.net mvc - How to pass values of checkboxes from JavaScript/view to controller in MVC? -
i working on form enables maximum of 11 football players selected list using checkboxes. separated , named based on playing position.
@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) }
i using following piece of javascript display each of selected values in order inside pop-up box when button clicked.
$('#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 });
however, want these values pass them through controller can record them in database. these checkboxes placed within partial view , not part of particular model why it's not straightforward (at least far can see).
basically, working apart not being able record ids of each player has been selected. thinking there must way of doing - either expanding on have done or using different approach.
ideally, record each of eleven values separately rather array.
here extract controller add values 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"); }
you pass checkboxes values in array , pass save controller , apply loop 1 one , store value in db.
array trans = new array(); trans.push($('input[type="checkbox"]:checked').eq(0).val());
then pass same array trans mvc action.
Comments
Post a Comment