【问题标题】:How to pass values of checkboxes from JavaScript/view to controller in MVC?如何将复选框的值从 JavaScript/视图传递到 MVC 中的控制器?
【发布时间】:2017-12-31 19:39:32
【问题描述】:

我正在制作一个表格,该表格允许使用复选框从列表中选择最多 11 名足球运动员。他们是根据他们的演奏位置分开和命名的。

@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)
}

我正在使用以下一段 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
});

但是,我真正想要对这些值做的是将它们传递给控制器​​,以便我可以将它们记录在数据库中。这些复选框放置在局部视图中,而不是该特定模型的一部分,这就是为什么它不是那么简单(至少据我所知)。

基本上,除了无法记录每个被选中玩家的 ID 之外,一切都按照我的意愿进行。我认为必须有一种方法可以做到这一点 - 要么扩展我所做的事情,要么使用不同的方法。

理想情况下,我想分别记录这 11 个值,而不是作为一个数组。

这是我的控制器的摘录,我想将值添加到我的表中(当前已注释掉):

   [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");
        }

【问题讨论】:

    标签: javascript asp.net-mvc checkbox view controller


    【解决方案1】:

    您将传递数组中的所有复选框值并将保存传递给控制器​​并一一应用循环并将值存储在数据库中。

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

    然后将相同的数组 trans 传递给 MVC 动作。

    【讨论】:

    • 感谢您的回复,但我不太确定所有内容应该如何组合在一起。什么需要去哪里?我从我的控制器中添加了一些进一步的代码,可以帮助您在这方面进行解释。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2014-08-24
    • 1970-01-01
    • 2016-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多