Disabled DropDownList razor mvc

In my razor mode, I use a drop-down list. I want this control to be disabled (not selectable).

My code is:

<div class="field-list">@Html.DropDownListFor(model => model.LinguaCodiceMadre, Model.LinguaMadreList, new{ @disabled = "disabled" })</div>

But this does not work, my control is always on. Html Page Code:

<select name="LinguaCodiceMadre" id="LinguaCodiceMadre" data-val-length-max="10" data-val-length="The field LinguaCodiceMadre must be a string with a maximum length of 10." data-val="true">
    <option></option>
    <option value="sq">Albanian</option>
    <option value="de">German</option>
    <option value="en">English</option>
    <option value="fr">French</option>
    <option value="it">Italian</option>
    <option value="pt">Portuguese</option>
    <option value="ru">Russian</option>
    <option value="es">Spanish</option>
</select>

without the disabled attribute.

My real goal is to enable / disable the dropdown menu conditionally, something like this:

<div class="field-list">@Html.DropDownListFor(model => model.LinguaCodiceMadre, Model.LinguaMadreList, new{@disabled=Model.IsDisabled ? "disabled" : "false"})</div>

but that will not work.

I tried both with

new{@disabled=Model.IsDisabled ? "disabled" : "false"}

and

new{disabled=Model.IsDisabled ? "disabled" : "false"}

but nothing, the disabled attribute is not displayed on the html page.

Does anyone have an idea?

+4
source share
3 answers

I solved my problem: in my code there was javascript (sorry, I did not notice right away) deleting the disable attribute when preparing the document.

What I've done:

  • HtmlHelper:

public static class HtmlExtensions
{
    public static MvcHtmlString DropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, string optionText, bool canEdit)
    {
        if (canEdit) return html.DropDownListFor(expression, selectList, optionText);
            return html.DropDownListFor(expression, selectList, optionText, new { @disabled = "disabled" });
    }
}
  • :

<code><div class="field-list">@Html.DropDownListFor(model => model.LinguaCodiceMadre, Model.LinguaMadreList, "", Model.IsEnabled)</div></code>

!

+1

.

<div class="field-list">@Html.DropDownListFor(model => model.LinguaCodiceMadre, Model.LinguaMadreList, new{ disabled = "disabled" })</div>

.

, - jquery:

@Id -

$("#youid").prop("disabled", true); 

, : ,

<div class="field-list">@Html.DropDownListFor(model => model.LinguaCodiceMadre, Model.LinguaMadreList,String.Empty, new{ disabled = "disabled" })</div>

+9

-, DropDown List , javascript / .

Javascript :

$("#checkbox1").change(function () {

  if (document.getElementById("checkbox1").checked == true) {

     document.getElementById("DropDown1").disabled = true;
  }
  else {

     document.getElementById("DropDown1").disabled = false;
  }

});

, ,

  • I fill in the test data in DropDown in the controller for demonstration (in your case it will be a database)
  • In the view, I assign identifiers to both inputs.
  • Finally, in the view below, there is a Javascript function that controls the CheckBox to see if wether is checked or not. If it is checked, you cannot select any values ​​from DropDown, otherwise it will be available for selecting values.
+3
source

Source: https://habr.com/ru/post/1543884/


All Articles