asp.net-mvc 使用 disabled="disabled" 属性创建 SelectListItem
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2655035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating a SelectListItem with the disabled="disabled" attribute
提问by Mark
I'm not seeing a way to create, via the HtmlHelper
, a SelectListItem
that will spit out the following HTML:
我没有看到通过HtmlHelper
, a创建SelectListItem
会吐出以下 HTML 的方法:
<option disabled="disabled">don't click this</option>
The only properties SelectListItem
has are:
唯一的属性SelectListItem
是:
new SelectListItem{
Name = "don't click this",
Value = string.Empty,
Selected = false
}
The only option I see is to
我看到的唯一选择是
- Subclass the
SelectListItem
to add anEnabled
property to get the value to the view - Not use the HTML helper for DropDownList
- Create a new
HtmlHelper
extension that accepts my newEnablableSelectList
and adds mydisabled
attribute.
- 子类化
SelectListItem
以添加Enabled
属性以获取视图的值 - 不使用DropDownList的HTML 帮助程序
- 创建一个
HtmlHelper
接受我的新扩展EnablableSelectList
并添加我的disabled
属性的新扩展。
采纳答案by Jab
This is something I might try before recreating the helper completely. The basic idea is that the Html you get from the helper should be well formed, so it should be safe to parse. So you can build on that idea by making your own extension that uses the existing extension but adds the functionality to disable the items.
这是我在完全重新创建助手之前可能会尝试的事情。基本思想是,您从帮助程序获得的 Html 应该格式良好,因此解析应该是安全的。因此,您可以通过创建自己的扩展来构建该想法,该扩展使用现有扩展但添加了禁用项目的功能。
Something like this might do (totally untested)
这样的事情可能会做(完全未经测试)
public class CustomSelectItem : SelectListItem
{
public bool Enabled { get; set; }
}
public static class CustomHtmlHelpers
{
public static MvcHtmlString MyDropDownList(this HtmlHelper html, IEnumerable<CustomSelectItem> selectList)
{
var selectDoc = XDocument.Parse(html.DropDownList("", (IEnumerable<SelectListItem>)selectList).ToString());
var options = from XElement el in selectDoc.Element("select").Descendants()
select el;
foreach (var item in options)
{
var itemValue = item.Attribute("value");
if (!selectList.Where(x => x.Value == itemValue.Value).Single().Enabled)
item.SetAttributeValue("disabled", "disabled");
}
// rebuild the control, resetting the options with the ones you modified
selectDoc.Root.ReplaceNodes(options.ToArray());
return MvcHtmlString.Create(selectDoc.ToString());
}
}
回答by Mihkel Müür
The Disabled
property is supported since ASP.NET MVC 5.2:
Disabled
从 ASP.NET MVC 5.2 开始支持该属性:
new SelectListItem {
// ...
Disabled = true
}
See the API reference.
请参阅API 参考。
回答by staccata
Clientside option: if you for example give your dropdownlist a class 'custom' and the items that should be unselectable the value -1 (for example), then you can do something like:
客户端选项:例如,如果您为下拉列表指定了一个“自定义”类,而不应选择的项目值为 -1(例如),那么您可以执行以下操作:
$('select.custom option[value=-1]').each(function () {
$(this).attr("disabled", "disabled");
});
回答by Thyamine
If all you are trying to do is prevent a user from selecting a certain value from the list, it seems like the simpler and more time-efficient way to do it is to use input validation. Which you may quite possibly be doing anyways, if you want to verify they've made a selection to begin with.
如果您要做的只是阻止用户从列表中选择某个值,那么使用输入验证似乎更简单、更省时。如果您想验证他们是否已经做出了选择,那么无论如何您很可能会这样做。
回答by Adel Mourad
-----Option 1 Controller:
-----选项1控制器:
var ExpectedShipmentsRange = new List();
var ExpectedShipmentsRange = new List();
ExpectedShipmentsRange.Add(new SelectListItem() { Text = "Selected number of shipments", Value="0", Disabled = true, Selected = true });
ExpectedShipmentsRange.Add(new SelectListItem() { Text = "0 to 20 shipments", Value = "0-20" });
ExpectedShipmentsRange.Add(new SelectListItem() { Text = "20 to 40 shipments", Value = "20-40" });
ViewBag.ExpectedShipmentsRange = ExpectedShipmentsRange;
View:
看法:
@Html.DropDownListFor(m => m.ExpectedShipments, (IEnumerable<SelectListItem>)@ViewBag.ExpectedShipmentsRange, new { @class = "form-control" })
-----Option 2 Controller:
-----选项2控制器:
ViewBag.citiesSa = _dbContext.Countries.ToList();
View:
看法:
@Html.DropDownListFor(m => m.City, new SelectList(@ViewBag.citiesSa, "Id", "Name"), "Select your city", new { @class = "form-control" })
-----Option 3 does not support disabled option:
-----选项3不支持禁用选项:
List<SelectListItem> ExpectedShipmentsRange = new List<SelectListItem>();
ExpectedShipmentsRange.Add(new SelectListItem() { Text = "0 to 20 shipments", Value = "0-20" });
ExpectedShipmentsRange.Add(new SelectListItem() { Text = "20 to 40 shipments", Value = "20-40" });
ViewBag.ExpectedShipmentsRange = new SelectList(ExpectedShipmentsRange, "Value", "Text");
View:
看法:
@Html.DropDownListFor(m => m.ExpectedShipments, (SelectList)@ViewBag.ExpectedShipmentsRange, new { @class = "form-control" })