禁用基于模型属性的启用下拉列表 在 mvc 中
Disable enable dropdownlistfor based on model property in mvc
我正在尝试根据模型属性在我的 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)
相关文章:
- 语义ui如何使用javascript启用或禁用下拉列表
- 我正在尝试使用jquery来启用下拉列表
- 如何根据是否通过jsp中的jstl选中单选按钮来启用和禁用下拉列表
- 如何强制用户从下拉列表和按钮中选择一个值以启用保存按钮
- 禁用基于下拉列表选择 asp.net JavaScript 启用文本框
- 根据下拉列表中的选定值启用文本框
- 如何根据所选下拉列表中的值启用和禁用表单控件
- 从禁用和启用的下拉列表中获取选定的值
- 如何重新启用禁用的下拉列表以及如何清除文本框数据
- 启用下拉列表仅在单击后悬停
- 如何在加载活动项时启用活动项以在页面的下拉列表中选中
- 禁用和启用Foundation下拉列表
- 下拉列表“;启用true”;Asp.net不起作用
- 我们如何使用SeleniumWebdriver处理启用Javascript的下拉列表
- 如何在ASP中使用JavaScript禁用/启用带有按钮的下拉列表
- 基于从辅助下拉列表中选定的选项启用下拉列表
- 在特定的单选按钮点击启用文本框/下拉列表在MVC使用jquery
- 如何启用/禁用输入类型="text"当从下拉列表中选择值时
- Ember JS仅在单击的元素上启用下拉列表
- 禁用基于模型属性的启用下拉列表 在 mvc 中