【问题标题】:Check if a checkbox is checked in a list of items检查是否在项目列表中选中了复选框
【发布时间】:2013-03-09 05:03:51
【问题描述】:

我正在构建一个 MVC 应用程序,现在我的视图会生成一组项目。用户要发送数据需要勾选复选框。

这是我的观点以及它是如何构建的:

<script type="text/javascript">
    $(document).ready(function() {
        //alert("The document is ready");
        $("#selectAll").click(function() {
            //alert("The case has been clicked");
            var chkValue = $(this).is(":checked");
            $(".divChckBox").prop("checked", chkValue);
        });
    });
</script>
<p>
    @using (Html.BeginForm("SendObj", "Manager"))
    {
        <p>
            Select / UnSelet All Items @Html.CheckBox("selectAll", true) 
        </p>
        <table>
            <tr>
                <th>Card Name</th>
                <th>Number In Stock</th>
                (...)
            </tr>
            @for (int i = 0; i < Model.Count(); i++)
            {
                <tr>
                    <td>@Html.DisplayFor(x => x[i].m_OthObj.m_ObjName)</td>
                    <td>@Html.DisplayFor(x => x[i].m_NbInStock)@Html.HiddenFor(x => x[i].m_NbInStock)</td>
                    (...)
                    <td>
                        <input type="checkbox" name="itdoesnotmatter" class="divChckBox" checked="true"/>
                    </td>
                </tr>
            }

        </table>
        <input type="submit" value="Send"/>
    }
</p>

所以你明白为什么我不能使用“CheckboxFor”。现在我要做的是只发送复选框状态为“已选中”的项目。我知道如何通过模型绑定(checkboxfor)来做到这一点,但我对如何构建它一无所知。 我需要返回一个项目列表。那么我该怎么做呢?非常感谢!

【问题讨论】:

  • 只需提供模型的 ID 作为复选框的值,然后接收 IList&lt;String&gt;(或 Int32Guid 等)并交叉引用选择和提交的那些. (另外,有趣的是,您有以 m_* 开头的公共属性,因为这通常是类成员(内部)的象征
  • 您是在问如何通过 POST 将其发送回您的控制器吗?
  • @MikeC。 : 是的,没错。

标签: c# asp.net-mvc html razor


【解决方案1】:

您的表单将返回基于名称的值,因此请射杀告诉您如此愚蠢名称的人 :)
使用

<input type="checkbox" name="InStock" class="divChckBox" checked="true" value="@Model[i].ID" />

或者更具代表性的东西。请注意,提供唯一标识符作为复选框的值是至关重要的。该值是您将如何识别检查的内容!

在您的控制器中,您可以通过多种方式捕获它。我是这样做的:

public ActionResult Create(List<int> InStock)
{
  foreach(var inStockItem in InStock)
  {
    //do what you need to do
  }
}

重点:

List<int> InStock

这必须与复选框上的 NAME 属性相匹配。实际值将是您的复选框的值。

在这里,我只是为您的操作随机选择了创建,但您需要使其与您正在进行的任何操作(编辑、索引等)相匹配。

祝你好运!

【讨论】:

  • 谢谢,我们会解决这个问题,并会尽快回复您。至于愚蠢的名字,它只是为了这个问题,别担心:)
  • 有效!非常感谢,你今天帮了大忙!
【解决方案2】:

尝试使用attr 方法更改属性checked

$(document).ready(function() {
        $("#selectAll").click(function() {
            var chkValue = $(this).is(":checked");
            $(".divChckBox").attr("checked", chkValue);
        });
    });

【讨论】:

    【解决方案3】:

    查看代码:

    <!-- note "x[i].m_id"; Use the entity's id property is here
         ...maybe this should be m_NbInStock? -->
    <input type="checkbox" name="selectedItems" value="@x[i].m_id" class="divChckBox" checked="true"/>
    

    控制器代码:

    public class Manager : Controller
    {
        /* ... */
        [HttpPost]
        public ActionResult SendObj(IList<Int32> selectedItems)
        {
          // Grab those items by their IDs found within `selectedItems` and perform
          // any processing necessary
    
          // ...
          //return View();
        }
        /* ... */
    }
    

    【讨论】:

      猜你喜欢
      • 2017-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-09
      • 1970-01-01
      • 2013-10-28
      • 2015-06-23
      相关资源
      最近更新 更多