我有一个Region
类是这样的:如何在dropdownlistfor上禁用父项?
public class Region
{
public int Id { get; set; }
public int Name { get; set; }
public int ParentId { get; set; }
}
而且Person
具有区域:
public class Person
{
public int Id { get; set; }
public int Name { get; set; }
public int SurName { get; set; }
public int RegionId { get; set; }
}
但是,它不像树节点。只有2层。国家及其子区域 - 城市。我使用引导模板。
我收集这个地区这样的名单:
Country1 //need to disable this
City1
City2
City3
Country2 //need to disable this
City1
City2
亲自创建行动:
Viewbag.Regions = new SelectList(MyRepository.LoadRegions(), "Id", "Name");
,并考虑:
@Html.DropDownListFor(model => model.RegionId, ViewBag.Regions as IEnumerable<SelectListItem>, "-", new { data_rel = "chosen", @id = "region" })
最后,我下拉打开时需要,要禁用国家,只能选择城市。
如何禁用dropdownlis中的元素parentId == 0
?
[创建一个SelectListItem与disabled =“disabled”属性]的可能的重复(http://stackoverflow.com/questions/2655035/creating-a-selectlistitem-with-the-disabled-disabled-attribute) –