Отключите раскрывающийся список на основе того, что определено в модели, также установите переключатель в положение Нет. - PullRequest
0 голосов
/ 13 июня 2018

У меня есть переключатель, который в модели выбран как Нет.Но я хочу, чтобы он автоматически отключал выпадающее меню.Но если вы выберете Да, то выпадающий список работает.Я знаю, что вы можете сделать это с JQuery.Но мне было интересно, если это возможно с 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>

Контроллер

`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
                }
            };`

Ответы [ 2 ]

0 голосов
/ 13 июня 2018

Я бы сделал это с 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>
0 голосов
/ 13 июня 2018

Это то, что вы имеете в виду?

<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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...