简体   繁体   English

根据模型中的定义禁用下拉列表,并将单选按钮设置为否

[英]Disable a dropdown based on what is defined in the Model also set Radio button to No

I have a Radio button, that is preselected as No in the model. 我有一个单选按钮,该按钮在模型中被预选为“否”。 But I want it to disable a dropdown menu automatically. 但我希望它自动禁用下拉菜单。 But if you select Yes then the dropdown works. 但是,如果选择“是”,则下拉列表将起作用。 I am aware that you can do it with JQuery. 我知道您可以使用JQuery做到这一点。 But I was wondering if its possible with C# MVC. 但是我想知道C#MVC是否可能。

        <div class="form-group dropdownSwitchContainer">
                        <label class="control-label col-md-2" for="TenantGroup">Is this a Proxy Organisation?</label>
                        <div class="col-lg-6">
                            <div class="admin-form theme-primary">
                                <div class="radio-custom radio-primary mt10 mr10 pull-left">
                                    @Html.RadioButtonFor(m => m.ProxyOrganisation, true, new { @class = "control-label col-md-4" })
                                    <label for="ProxyYes">Yes</label>
                                </div>
                                <div class="radio-custom radio-primary mt10 mr10 pull-left">
                                    @Html.RadioButtonFor(m => m.ProxyOrganisation, true, new { @class = "control-label col-md-4" })
                                    <label for="ProxyYes">No</label>
                                </div>
                            </div>
                        </div>
                    </div>

                    @{
    var emptyList = new List<SelectListItem>();
    var proxiesList = new SelectList(emptyList);
    if (proxies != null)
    {
        proxiesList = new SelectList(proxies, "key", "value", 0);
    }
                    }
                    <div class="form-group">
                        <label class="control-label col-md-2" for="AlternativeShippingTarget">Alternative Shipping Target</label>
                        <div class="col-md-8">
                            @Html.DropDownListFor(x => x.ShippingTarget, proxiesList, "Choose a target...", new { id = "targetSelect", @class = "form-control", placeholder = "Choose a target..." })
                            @Html.ValidationMessageFor(m => m.ShippingTarget, string.Empty, new { @class = "text-danger" })
                        </div>
                    </div>
                </div>

Controller 调节器

`var model = new ViewModels.SocietiesCreateModel
            {
                SocietyName = string.Empty,
                CISACCode = string.Empty,
                ShippingTarget = string.Empty,
                ProxyOrganisation = false,

                PagingInfo = new ViewModels.PagingModel
                {
                    Page = page,
                    Take = take,

                    SortAscending = sortAsc,
                    SortPropertyName = sortBy
                }
            };`

Is this what you mean? 你是这个意思吗?

 <form action=""> <label for="no"> No <input checked id="no" name="option" type="radio" id="no"/> </label> <label for="yes"> Yes <input name="option" type="radio" id="yes"/> </label> <select id="dropdown"> <option value="1">option 1</option> <option value="2">option 2</option> </select> </form> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script> function toggleDropdown(){ if ($("#no").is(":checked")) { $('#dropdown').prop('disabled', true); } else { $('#dropdown').prop('disabled', false); } } $(document).ready(function(){ toggleDropdown(); }); $('input').change(function(){ toggleDropdown(); }); </script> 

I would do it with JQuery but you could try something like this without Jquery 我会用JQuery做到这一点,但如果没有Jquery,您可以尝试类似的方法

<div class="form-group">
    <label class="control-label col-md-2" for="AlternativeShippingTarget">Alternative Shipping Target</label>
    <div class="col-md-8">
        @{
            object htmlAttributes;
            if (Model.ProxyOrganisation)
            {
                htmlAttributes = new { id = "targetSelect", @class = "form-control", placeholder = "Choose a target..." };
            }
            else
            {
                htmlAttributes = new { id = "targetSelect", @class = "form-control", placeholder = "Choose a target...", disabled = "disabled" };
            }



            @Html.DropDownListFor(x => x.ShippingTarget, proxiesList, "Choose a target...", htmlAttributes)
            @Html.ValidationMessageFor(m => m.ShippingTarget, string.Empty, new { @class = "text-danger" })
        }
    </div>
</div>

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

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