javascript - 如何将复选框的值从 JavaScript/ View 传递到 MVC 中的 Controller ?

标签 javascript asp.net-mvc checkbox view controller

我正在开发一种表单,可以使用复选框从列表中最多选择 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
});

但是,我真正想要处理这些值的是将它们传递到 Controller ,以便我可以将它们记录在数据库中。这些复选框放置在部分 View 中,而不是该特定模型的一部分,这就是为什么它不那么简单(至少据我所知)。

基本上,除了无法记录已选择的每个玩家的 ID 之外,一切都按我希望的方式进行。我认为必须有一种方法可以做到这一点 - 要么通过稍微扩展我所做的事情,要么使用不同的方法。

理想情况下,我想单独记录这十一个值,而不是作为数组。

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

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

最佳答案

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

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

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

关于javascript - 如何将复选框的值从 JavaScript/ View 传递到 MVC 中的 Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45310738/

相关文章:

javascript - jquery切换只执行第一个条件

javascript - 如何在 Angular 组件中绑定(bind)任何特定的对象属性

javascript - laravel 仅在我需要的页面上加载 vuejs 组件

c# - 从 Controller 生成 http post 请求

asp.net-mvc - 如何使用 1 个解决方案实现对 2 个网站的支持

angular - 禁用 PrimeNG 复选框

javascript - 循环遍历表行,获取总计并更新行

javascript - For 循环和数组问题 - ngRepeat

c# - 获取上传文件的大小 C# MVC

R:shiny updateCheckboxGroup 按下时更新