简体   繁体   English

在 ASP.NET MVC 中获取复选框值

[英]Getting Checkbox Value in ASP.NET MVC

I'm working on an ASP.NET MVC app.我正在开发一个 ASP.NET MVC 应用程序。 This app has a basic form.这个应用程序有一个基本的形式。 The model for my form looks like the following:我的表单模型如下所示:

public partial class User
{
    public bool Addon { get; set; }
}

In my form, I have the following HTML.在我的表单中,我有以下 HTML。

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

I currently don't have any code for the Addon in the controller.我目前在控制器中没有任何关于插件的代码。

I want to check if Checkbox was checked inside a different HTML to show hidden links that would only be available if the checkbox was checked.我想检查 Checkbox 是否在不同的 HTML 中被选中以显示只有在选中复选框时才可用的隐藏链接。

How would I able to check it if the if statement was in a different HTML page?如果 if 语句位于不同的 HTML 页面中,我将如何检查它? Because the checkbox would be checked at the moment at User Creation.因为该复选框将在用户创建的那一刻被选中。 Then once logging in, the index page would depend if the checkbox it checked or not.然后一旦登录,索引页面将取决于它是否选中了复选框。

An example would be一个例子是

if (Addon == True)
{
    <div class="col-md-4">
        <p><a onclick="@("window.location.href='" + @Url.Action("Create", 
        "Users") + "'");" class="btn btn-primary btn-lg">Add User &raquo; 
         </a></p>
     </div>
}

Any ideas on how I could go about this?关于我如何解决这个问题的任何想法? Thank you.谢谢你。

You can set id of checkbox and use Javascript to control if it's check like this:您可以设置复选框的id并使用 Javascript 来控制它是否是这样检查的:

@Html.EditorFor(model => model.Addon, new { id ="chkAddon" })

Then use is(":checked") in Javascript would be like:然后在 Javascript 中使用is(":checked")就像:

if ($('#chkAddon').is(":checked"))
{
    <div class="col-md-4">
        <p><a onclick="@("window.location.href='" + @Url.Action("Create", 
        "Users") + "'");" class="btn btn-primary btn-lg">Add User &raquo; 
         </a></p>
    </div>
}
<div class="form-group">
    @Html.LabelFor(model => model.Addon, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        <div class="checkbox">
            @Html.EditorFor(model => model.Addon,"", new { @class = "check-btn" })
            @Html.ValidationMessageFor(model => model.Addon, "", new { @class = "text-danger" })
        </div>
    </div>
</div>

<a id="link" href="#"  style="display:none;">Link</a>

<script type="text/javascript">

    $(document).ready(function () {

        $(".check-btn").change(function () {
            if (this.checked) {
                $("#link").css("display", "block");
            }
            else {
                $("#link").css("display", "none");
            }
        });


</script>

you want to safety, need you use jquery ajax.你想要安全,需要你使用jquery ajax。

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

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