首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在MVC中将复选框值从JavaScript/view传递给控制器?

如何在MVC中将复选框值从JavaScript/view传递给控制器?
EN

Stack Overflow用户
提问于 2017-07-26 02:09:32
回答 1查看 398关注 0票数 1

我正在开发一个表单,可以使用复选框从列表中选择最多11名足球运动员。他们是分开的,并根据他们的踢球位置命名。

代码语言:javascript
复制
@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在弹出框中按顺序显示每个选定值。

代码语言: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个值中的每个值,而不是以数组的形式记录。

下面是我的控制器中的一段摘录,我希望将这些值添加到我的表中(当前已被注释掉):

代码语言:javascript
复制
   [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");
        }
EN

回答 1

Stack Overflow用户

发布于 2017-07-26 02:17:44

您将以数组的形式传递所有复选框值,并将save传递给控制器,然后逐个应用循环并将值存储在DB中。

代码语言:javascript
复制
Array trans = new Array();
trans.push($('input[type="checkbox"]:checked').eq(0).val());

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45310738

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档