代码之家  ›  专栏  ›  技术社区  ›  ETFairfax

html.dropdownlist-禁用/只读

  •  59
  • ETFairfax  · 技术社区  · 15 年前

    当使用mvcs html.dropdownlist时,需要设置什么选项使下拉框为只读?

    我试过像……

    Html.DropDownList("Types", Model.Types, new { _Enabled = "false" })
    

    ……还有很多不同的事情,唉,没有快乐!

    我以为这很容易……而且很可能!

    12 回复  |  直到 6 年前
        1
  •  120
  •   Jamie Rees    9 年前

    试试这个

    Html.DropDownList("Types", Model.Types, new { @disabled = "disabled" })
    
        2
  •  72
  •   Jamie Rees    9 年前

    关于第22条:

    如果我们使用 @disabled ,字段不发送到操作(mamoud) 如果我们使用 @readonly ,下拉错误仍然允许您更改值

    解决办法:使用 @残疾人 ,并添加下拉列表后隐藏的字段:

    @Html.HiddenFor(model => model.xxxxxxxx)
    

    然后它就真的被禁用了,并被发送到操作。

        3
  •  6
  •   Muhammad Mubashir    12 年前
    <script type="text/javascript">
    $(function () {
            $(document) .ajaxStart(function () {
                    $("#dropdownID").attr("disabled", "disabled");
                })
                .ajaxStop(function () {
                    $("#dropdownID").removeAttr("disabled");
    
                });
    
    });
    </script>
    
        4
  •  3
  •   Steven Rumbalski    12 年前

    或者你可以尝试这样的方法:

    Html.DropDownList("Types", Model.Types, new { @readonly = "true" })
    
        5
  •  3
  •   cagedwhale    7 年前

    我必须禁用DropDownlist并隐藏主ID

    <div class="form-group">
            @Html.LabelFor(model => model.OBJ_ID, "Objs", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("OBJ_ID", null, htmlAttributes: new { @class = "form-control", @disabled = "disabled"})
                @Html.HiddenFor(m => m.OBJ_ID)
                @Html.ValidationMessageFor(model => model.OBJ_ID, "", new { @class = "text-danger" })
            </div>
        </div>
    
        6
  •  2
  •   Dwhitz Brett Porter    7 年前

    对某些人来说可能是显而易见的,但对其他人却不是。

    如果您使用的HTML助手基于 DropDownListFor 然后你的身份证将在 HiddenFor 输入。因此,您将拥有在HTML中无效的重复ID,如果您使用javascript填充 隐匿 DropDownList 那你就有麻烦了。

    解决方案是在htmlattributes数组中手动设置id属性…

    @Html.HiddenFor(model => model.Entity)
    
    @Html.EnumDropDownListFor(
      model => model.Entity, 
      new { 
             @class = "form-control sharp", 
             onchange = "", 
             id =` "EntityDD", 
             disabled = "disabled" 
           }
    )
    
        7
  •  1
  •   lollmbaowtfidgafgtfoohwtbs    8 年前

    我只是做了这件事,就到此为止

    Model.Id > -1 ? Html.EnumDropDownListFor(m => m.Property, new { disabled = "disabled" }) : Html.EnumDropDownListFor(m => m.Property)
    
        8
  •  1
  •   kirk    7 年前

    把这个放在风格上

     select[readonly] option, select[readonly] optgroup {
            display: none;
        }
    
        9
  •  0
  •   Andrew Day    7 年前
    @Html.DropDownList("Types", Model.Types, new { @disabled = "" })
    

    作品

        10
  •  0
  •   Roger Tello    7 年前

    html.dropdownlist(“类型”,model.types,new@disabled=“disabled”) @html.hidden(model.types) 为了保存和恢复数据,请使用隐藏控件

        11
  •  0
  •   Antonio Bakula    7 年前

    为完整起见,这里是DropDownListfor的HTML助手,它在禁用false select时添加了启用的参数。它保留在标记中定义的HTML属性,或者允许在标记中使用HTML属性,它向服务器发布选择值,并且使用非常干净和简单。

    以下是帮助程序的代码:

    public static MvcHtmlString DropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, object htmlAttributes, bool enabled)
    {
      if (enabled)
      {
        return SelectExtensions.DropDownListFor<TModel, TProperty>(html, expression, selectList, htmlAttributes);
      }
    
      var htmlAttributesAsDict = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
      htmlAttributesAsDict.Add("disabled", "disabled");
      string selectClientId = html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(ExpressionHelper.GetExpressionText(expression));
      htmlAttributesAsDict.Add("id", selectClientId + "_disabled");
    
      var hiddenFieldMarkup = html.HiddenFor<TModel, TProperty>(expression);
      var selectMarkup = SelectExtensions.DropDownListFor<TModel, TProperty>(html, expression, selectList, htmlAttributesAsDict);
      return MvcHtmlString.Create(selectMarkup.ToString() + Environment.NewLine + hiddenFieldMarkup.ToString());
    }
    

    和用法,目标是禁用选择,如果选项中只有一个项目,则标记:

    @Html.DropDownListFor(m => m.SomeValue, Model.SomeList, new { @class = "some-class" }, Model.SomeList > 1)
    

    还有一个更优雅的HTML助手示例,目前不支持Post(非常直接的工作,只使用hap并添加隐藏输入作为根元素sibling和swap id):

    public static MvcHtmlString Disable(this MvcHtmlString previous, bool disabled, bool disableChildren = false)
    {
      if (disabled)
      {
        var canBeDisabled = new HashSet<string> { "button", "command", "fieldset", "input", "keygen", "optgroup", "option", "select", "textarea" };
        var doc = new HtmlDocument();
        doc.LoadHtml(previous.ToString());
        var rootElements = doc.DocumentNode.Descendants().Where(
          hn => hn.NodeType == HtmlNodeType.Element && 
          canBeDisabled.Contains(hn.Name.ToLower()) && 
          (disableChildren || hn.ParentNode.NodeType == HtmlNodeType.Document));
    
        foreach (var element in rootElements)
        {
          element.SetAttributeValue("disabled", "");
        }
        string html = doc.DocumentNode.OuterHtml;
        return MvcHtmlString.Create(html);
      }
      return previous;
    }
    

    例如,有一个模型属性bool all inputs disabled,如果为true,则应禁用所有HTML输入:

    @Html.TextBoxFor(m => m.Address, new { placeholder = "Enter address" }).Disable(Model.AllInputsDisabled)
    
    @Html.DropDownListFor(m => m.DoYou, Model.YesNoList).Disable(Model.AllInputsDisabled)
    
        12
  •  -1
  •   Tharanga    8 年前

    我在参考了以上所有评论和答案后创建了此答案。 这将解决下拉填充错误,即使它被禁用。

    步骤01

    Html.DropDownList("Types", Model.Types, new {@readonly="readonly"})

    步骤02 这是CSS指针事件移除代码。

    <style type="text/css">
        #Types {
            pointer-events:none;
        }
    </style>
    那么你就可以得到预期的结果了

    经过测试和验证