【问题标题】:Disable enable dropdownlistfor based on model property in mvc基于 mvc 中的模型属性禁用启用下拉列表
【发布时间】:2016-07-06 14:05:04
【问题描述】:

我正在尝试根据模型属性在我的 mvc 应用程序中禁用或启用下拉列表:-

我正在做的是:-

@Html.DropDownListFor(m => m.ParentOrganisationID, new SelectList(Model.ParentOrganisations, "ID", "Name", Model.ParentOrganisationID), new { @id = "ddlParentOrganisations", @class = "form-control css-select", @disabled = Model.IsReadOnly ? "disabled" : "false", @style = "width:40%; height:10%;" })

但即使模型属性“model.IsReadOnly”为假,它也会将下拉菜单显示为禁用。

请建议如何在不使用任何 javascript 的情况下处理此问题

提前致谢

【问题讨论】:

    标签: javascript asp.net-mvc html.dropdownlistfor


    【解决方案1】:

    不可能在对DropDownListFor 辅助方法的调用中包含条件(if/ternary statement(s)),因为您不能在它需要一个对象的地方传递一行 c# 代码(带有您的 if 条件)用于 html 属性。此外,以下所有标记都将呈现禁用的 SELECT。

    <select disabled></select>
    <select disabled="disabled"></select>
    <select disabled="false"></select>
    <select disabled="no"></select>
    <select disabled="usingJqueryEnablePlugin"></select>
    <select disabled="enabled"></select>
    

    您可以简单地使用 if 条件检查您的 Model 属性的值,并有条件地呈现禁用的版本。

    @if (!Model.IsReadOnly)
    {
        @Html.DropDownListFor(s => s.ParentOrganisationID, 
                                   new SelectList(Model.ParentOrganisations, "ID", "Name"))
    }
    else
    {
        @Html.DropDownListFor(s => s.ParentOrganisationID, 
           new SelectList(Model.ParentOrganisations, "ID", "Name"),new {disabled="disabled"})
    }
    

    您可以考虑创建一个自定义 html 帮助器方法来处理 if 条件检查。

    public static class DropDownHelper
    {
        public static MvcHtmlString MyDropDownListFor<TModel, TProperty>
                     (this HtmlHelper<TModel> htmlHelper, 
                      Expression<Func<TModel, TProperty>> expression,
                      IEnumerable<SelectListItem> selectItems,
                      object htmlAttributes,
                      bool isDisabled = false)
        {
            ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression,
                                                                        htmlHelper.ViewData);
    
            IEnumerable<SelectListItem> items =
                selectItems.Select(value => new SelectListItem
                {
                    Text = value.Text,
                    Value = value.Value,
                    Selected = value.Equals(metadata.Model)
                });
            var attributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
            if (isDisabled && !attributes.ContainsKey("disabled"))
            {
                 attributes.Add("disabled", "disabled");
            }
            return htmlHelper.DropDownListFor(expression,items, attributes);
        }
    }
    

    现在在你的剃刀视图中,调用这个助手

    @Html.MyDropDownListFor(s=>s.ParentOrganisationID,
                   new SelectList(Model.ParentOrganisations, "ID", "Name")
                                               ,new { @class="myClass"},Model.IsReadOnly)
    

    【讨论】:

    • 我有多个 dropdownlistfor 在我看来大约有 20 个,所以对于所有这些我都必须写这个条件......没有任何解决方法
    • @tanujshrivastava 是的。您可以创建一个自定义助手来执行此操作。检查我更新的答案。
    • @shyju- 但我也在新的 {html 属性} 中提供 id 和 class,以你的方式我将无法通过它们
    • 我的 MVC 客户端验证在使用这个 html.extension 时不起作用
    【解决方案2】:

    这是一条 HTML 基本规则:从您设置属性 disabled(无论其值如何)的那一刻起,该元素将被禁用。

    要得到你想要的,你需要创建一个 HTML 扩展 DropDownListFor

    请看this link.

    【讨论】:

      【解决方案3】:

      Shyju 接受的答案效果很好。但是,如果您想在自定义助手中使用 HTML5 data-* 属性怎么办?标准 MVC DropDownListFor 通过使用下划线 (_) 代替破折号 (-) 提供了一种解决方法。并且该助手足够智能,可以在渲染标记时将下划线转换为破折号。

      这是一个自定义助手,它将提供一个参数来禁用 DropDownList 并适当地转换 HTML5 data-* 属性。它还保留通过 htmlAttributes 参数传入的任何其他值。代码也更简洁一些(imo)。

          public static MvcHtmlString MyDropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> list, string optionLabel, object htmlAttributes, bool disabled)
          {
              var routeValues = new System.Web.Routing.RouteValueDictionary();
      
              // convert underscores to dashes...
              foreach (System.ComponentModel.PropertyDescriptor property in System.ComponentModel.TypeDescriptor.GetProperties(htmlAttributes))
              {
                  routeValues.Add(property.Name.Replace('_', '-'), property.GetValue(htmlAttributes));
              }
      
              if(disabled)
              {
                  routeValues.Add("disabled", "disabled");
              }
      
              return htmlHelper.DropDownListFor(expression, list, optionLabel, routeValues);
          }
      

      还有标记:

      @Html.MyDropDownListFor(m =&gt; m.MonthId, Model.Months, "Select Month", new { @class = "form-control", data_url = Url.Action("GetMonths") }, Model.IsDisabled)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多