复选框通过MVC 5中的ID(视图/控制器)取消行

时间:2014-10-02 00:59:52

标签: html asp.net-mvc checkbox asp.net-mvc-5

我的视图中有一个表格,其中包含一列复选框。我希望用户能够选择他们喜欢的复选框,并单击底部的按钮以停用它们(此模型中的用户可以暂停/停用并恢复/激活)。我没有任何错误,但是当我点击按钮时什么也没发生。我在下面附上了我的代码。感谢。

查看

@using (Html.BeginForm("CheckBoxDeactivate"))
{ 
<table class="table">
<tr>
    <th>
    Select Users
    </th>
    <th>
        @Html.DisplayNameFor(model => model.ID)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.FirstName)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.LastName)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Email)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.UserName)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.suspended)
    </th>

    <th></th>
</tr>
@foreach (var item in Model) {
<tr>
    <td>
        <input type= "checkbox" name="manageUsers" value="@item.ID" />
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.ID)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.FirstName)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.LastName)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Email)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.UserName)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.suspended)
    </td>

</tr>
}
</table>
<br />
<input type="button" value="Add Roles" onclick="location.href'@Url.Action("CheckBoxDeactivate", "User")'" />



控制器

[HttpPost]
        public ActionResult CheckBoxDeactivate(int[] ids)
        {
        tbl_Reg_User OAWUser = new tbl_Reg_User();
        foreach (var id in ids)
        {
            OAWUser = db.tbl_Reg_User.Single(o => o.ID == id);
            OAWUser.suspended = true;
            db.SaveChanges();   
        }
ViewBag.Message = "Users deactivated successfully!";
        return View();
    }

1 个答案:

答案 0 :(得分:0)

<input type= "checkbox" name="manageUsers" value="@item.ID" />

此行应为

<input type= "checkbox" name="ids" id="ids" value="@item.ID" />

我认为你没有在控制器中获得所选的复选框。

查看

@using (Html.BeginForm("CheckBoxDeactivate","Home"))
{ 
    <table class="table" border="1" style="border:1px solid black;border-collapse:collapse;">
        <tr>
            <th>Select Users
            </th>
            <th>
                Sr.No.
            </th>
            <th>
                FirstName
            </th>
            <th>
                LastName
            </th>
            <th>
                Phone
            </th>
            <th>
                IsActive
            </th>
        </tr>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    <input type= "checkbox" name="ids" id="ids" value="@item.Sr_No" />
                </td>
                <td>
                    @item.Sr_No
                </td>
                <td>
                    @item.FirstName
                </td>
                <td>
                    @item.LastName
                </td>
                <td>
                    @item.Phone
                </td>
                <td>
                    @item.IsActive
                </td>
            </tr>
        }
</table>

    <br />
    <input type="submit" value="Add Roles" onclick="@Url.Action("CheckBoxDeactivate", "User")" />
}

<强>控制器

[HttpPost]
public ActionResult CheckBoxDeactivate(int[] ids)
    {
        Temp OAWUser = new Temp();
        if (ids != null)
        {
            foreach (var id in ids)
            {

                //Your code goes here
            }
            ViewBag.Message = "Users deactivated successfully!";
        }
        return RedirectToAction("Temp");
    }

这段代码为我做了工作