ASP.Net MVC6复选框值到控制器

时间:2017-07-10 15:09:59

标签: c# asp.net-mvc checkbox

我正在尝试单独创建一个复选框,当它单击它时,它将根据结果将真值或假值发送回控制器,它将添加userId或删除它并根据此返回结果。我没有使用模型,只是在点击提交请求后我就没有按钮。

到目前为止,我有:

@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
   <label>View All Tickets @Html.CheckBox("chkAllTickets")</label>
}

我只是想发回一些类似的东西 公共ActionResult索引(bool Checked)和单击后检查将保持true或false。

我是否需要使用jQuery或者它更简单,我只是缺少一些东西。到目前为止,我已轻松完成所有工作,这给我带来了很多麻烦。

提前致谢。

2 个答案:

答案 0 :(得分:0)

嗨,亚当,如果我正确理解你的问题,这很简单,也就是说,你可以使用控制器动作方法中的复选框的名称,并根据选中或未选中获得真或假。

<强>例如:

 @using (Html.BeginForm("Index", "Home", FormMethod.Post))
    {
       <label>View All Tickets @Html.CheckBox("chkAllTickets")</label>
    }

在控制器中:

Public class HomeController:Controller
{
   public actionresult Index(bool chkAllTickets)
    {
            //other logic
    }

}

注意:根据绑定到复选框的值,在操作方法中指定您的数据类型。

答案 1 :(得分:0)

我解决了复选框的问题。

<div style="display:inline-block">
    <label style="margin-left:10px;">
        <span><label>View All Tickets @Html.CheckBox("chkAllTickets")</label></span>
    </label>
</div>

我使用javascript和jquery来解决这个问题。 如果它对1动作是真的,那么对另一个动作是假的。

$(document).ready(function () {
        $('#chkAllTickets').on('change', function () {
            var theUrl = '';
            this.value = this.checked ? 1 : 0;
            if (this.value == 1)
            {
                theUrl = '@Html.Raw(Url.Action("Index", "Home", new { SortField = ViewBag.SortingPagingInfo.SortField, SortDirection = ViewBag.SortingPagingInfo.SortDirection, ChkAllTickets = true }))';
                window.location = theUrl;
            }
            else
            {
                theUrl =  '@Html.Raw(Url.Action("Index", "Home", new { SortField = ViewBag.SortingPagingInfo.SortField, SortDirection = ViewBag.SortingPagingInfo.SortDirection, ChkAllTickets = false }))';
                window.location = theUrl;
            }

        });
    });
相关问题