简体   繁体   English

MVC中有两个复选框,但一次只允许允许一个

[英]Two checkboxes in MVC, but only want to allow one at a time

In my model I have two bool properties, Approved and Rejected. 在我的模型中,我有两个布尔属性,批准和拒绝。 I would like them either as radio buttons or check boxes on the MVC view, but only want one to be selected at a time. 我希望将它们作为单选按钮或MVC视图上的复选框,但只希望一次选择一个。

Currently my view looks like this: 目前,我的视图如下所示:

<div class="form-group">
    @Html.LabelFor(model => model.Approved, htmlAttributes: new {@class = "control-label col-md-2"})
    <div class="col-md-2">
        <div class="checkbox">
            @Html.EditorFor(model => model.Approved, new { onclick="SingleCheckbox(this)" })
            @Html.ValidationMessageFor(model => model.Approved, "", new {@class = "text-danger"})
        </div>
    </div>
</div>

<div class="form-group">
    @Html.LabelFor(model => model.Rejected, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-2">
        <div class="checkbox">
            @Html.EditorFor(model => model.Rejected, new { onclick="SingleCheckbox(this)" })
            @Html.ValidationMessageFor(model => model.Rejected, "", new { @class = "text-danger" })
        </div>
    </div>
</div>

And the javascript looks like this: 和JavaScript看起来像这样:

SingleCheckbox = function(rb) {
    debugger;
    var id = rb.id, uncheck;
    if (id == 'Approved') {
        uncheck = document.getElementById('Rejected');
    } else {
        uncheck = document.getElementById('Approved');
    }
};

But when I inspect the element, it doesn't even have the onclick event. 但是当我检查元素时,它甚至没有onclick事件。

I've updated my code to reflect your recommendations (see above) and still have the same issue. 我已经更新了代码以反映您的建议(请参见上文),但仍然存在相同的问题。 The issue is that when I inspect the element the check box inputs DO NOT have an onclick event. 问题是,当我检查元素时,复选框输入中没有onclick事件。

Here is the rendered HTML: 这是呈现的HTML:

<div class="form-group">
    <label class="control-label col-md-2" for="Approved">Approved</label>
    <div class="col-md-2">
        <div class="checkbox">
            <input class="check-box" id="Approved" name="Approved" type="checkbox" value="true"><input name="Approved" type="hidden" value="false">
            <span class="field-validation-valid text-danger" data-valmsg-for="Approved" data-valmsg-replace="true"></span>
        </div>
    </div>
</div>
<div class="form-group">
    <label class="control-label col-md-2" for="Rejected">Rejected</label>
    <div class="col-md-2">
        <div class="checkbox">
            <input class="check-box" id="Rejected" name="Rejected" type="checkbox" value="true"><input name="Rejected" type="hidden" value="false">
            <span class="field-validation-valid text-danger" data-valmsg-for="Rejected" data-valmsg-replace="true"></span>
        </div>
    </div>
</div>

Call SingleCheckbox(this) instead of SingleCheckbox.call(this) in onclick of chackbox like following. 如下所示在chackbox的onclick中调用SingleCheckbox(this)而不是SingleCheckbox.call(this)

@Html.CheckBoxFor(model => model.Approved, new { onclick = "SingleCheckbox(this)" })
@Html.CheckBoxFor(model => model.Rejected, new { onclick = "SingleCheckbox(this)" })

And SingleCheckbox should be like following. SingleCheckbox应该如下所示。

var SingleCheckbox = function (rb) {
    var id = rb.id, uncheck;
    if (id == 'Approved') {
        uncheck = document.getElementById('Rejected');
    } else {
        uncheck = document.getElementById('Approved');
    }

    uncheck.checked = false;
}

Update: In asp.net mvc4 and it's earlier version @Html.EditorFor has no parameter for htmlAtrributes . 更新:在asp.net mvc4及其早期版本中, @Html.EditorFor没有htmlAtrributes参数。 You can use @Html.CheckBoxFor instead. 您可以改用@Html.CheckBoxFor

Hope this will help you. 希望这会帮助你。

The issue seemed to be with the @Html.EditorFor helper. 问题似乎与@Html.EditorFor帮助程序有关。 I changed my code and now it's working. 我更改了代码,现在可以正常工作了。

<div class="form-group">
    @Html.LabelFor(model => model.Approved, htmlAttributes: new {@class = "control-label col-md-2"})
    <div class="col-md-2">
        @Html.CheckBoxFor(model => model.Approved, new { onclick = "SingleCheckbox(this)" })
        @Html.ValidationMessageFor(model => model.Approved, "", new {@class = "text-danger"})
    </div>
</div>

<div class="form-group">
    @Html.LabelFor(model => model.Rejected, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-2">
        @Html.CheckBoxFor(model => model.Rejected, new { onclick = "SingleCheckbox(this)" })
        @Html.ValidationMessageFor(model => model.Rejected, "", new {@class = "text-danger"})
    </div>
</div>

I just have to update the javascript to be more generic, I may want to use this in other areas of the site. 我只需要更新JavaScript使其更通用,我可能想在网站的其他区域中使用它。

希望尝试此操作,它将在其中具有onclick的情况下呈现所需的标记

@Html.EditorFor(model => model.Approved, new { @onclick = '"SingleCheckbox.call(this)"' })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM