禁用基于模型属性的启用下拉列表 在 mvc 中

Disable enable dropdownlistfor based on model property in mvc

本文关键字:启用 下拉列表 mvc 属性 于模型 模型      更新时间:2023-09-26

我正在尝试根据模型属性在我的 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%;" })

但即使模型属性"模型。IsReadOnly"为假,然后它也将下拉列表显示为禁用。

请建议如何处理这个问题,而不使用任何javascript

提前致谢

在对 DropDownListFor 帮助程序方法的调用中无法包含条件(if/三元语句),因为无法将一行 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)

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

要获得所需的内容,您需要创建一个 HTML 扩展DropDownListFor

请参阅此链接。

Shyju 接受的答案效果很好。但是,如果要在自定义帮助程序中使用 HTML5 数据 * 属性,该怎么办?标准 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 => m.MonthId, Model.Months, "Select Month", new { @class = "form-control", data_url = Url.Action("GetMonths") }, Model.IsDisabled)