简体   繁体   English

如何将复选框的值从JavaScript /视图传递到MVC中的控制器?

[英]How to pass values of checkboxes from JavaScript/view to controller in MVC?

I am working on a form which enables a maximum of 11 football players to be selected from a list using checkboxes. 我正在使用一种表格,使用复选框可以从列表中选择最多11名足球运动员。 They are separated and named based on their 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 am using the following piece of JavaScript to display each of the selected values in order inside a pop-up box when a button is clicked. 我使用以下JavaScript来在单击按钮时在弹出框中按顺序显示每个选定的值。

$('#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, what I really want to do with these values is pass them through to the controller so that I can record them in the database. 但是,我真正想使用这些值的方法是将它们传递给控制器​​,以便可以将它们记录在数据库中。 These checkboxes are placed within a partial view and are not part of this particular model which is why it's not so straightforward (at least as far as I can see). 这些复选框位于部分视图中,而不是此特定模型的一部分,这就是为什么它不是那么简单的原因(至少在我看来)。

Basically, everything is working as I would like apart from not being able to record the IDs of each player that has been selected. 基本上,除了无法记录已选择的每个玩家的ID之外,一切都按我的意愿进行。 I am thinking that there must be a way of doing this - either by expanding on what I have done slightly or by using a different approach. 我认为必须有一种方法可以做到这一点-通过略微扩展我所做的事情或使用其他方法。

Ideally, I would like to record each of the eleven values separately rather than as an array. 理想情况下,我想单独记录这11个值,而不是将其记录为数组。

Here is an extract from my controller where I would like to add the values to 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");
        }

You will pass all checkboxes values in an array and pass save to the controller and apply the loop one by one and store the value in DB. 您将在数组中传递所有复选框的值,并将保存传递到控制器,并逐一应用循环,并将该值存储在DB中。

Array trans = new Array();
trans.push($('input[type="checkbox"]:checked').eq(0).val());

Then pass same array trans to MVC action. 然后将相同的数组trans传递给MVC动作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM