Asp.net mvc 3 当我设置SelectedItem时,为什么MVC3应用程序中的Dropdownlist被禁用?

Asp.net mvc 3 当我设置SelectedItem时,为什么MVC3应用程序中的Dropdownlist被禁用?,asp.net-mvc-3,Asp.net Mvc 3,我有一个MVC3应用程序,我定义了一个视图,可以传递值并设置SelectedItem值 List<SelectListItem> items = new SelectList(db.BILLING_COUNTRY, "ISO_Code_BillingCountry", "CountryName", Country).AsParallel().ToList(); items.Insert(0, (new SelectListItem { Text = "Select Y

我有一个MVC3应用程序,我定义了一个视图,可以传递值并设置SelectedItem值

List<SelectListItem> items = new SelectList(db.BILLING_COUNTRY, "ISO_Code_BillingCountry", "CountryName", Country).AsParallel().ToList();
        items.Insert(0, (new SelectListItem { Text = "Select Your Country", Value = "0" }));
        ViewBag.Countries = items;
我将ViewBag.EnableDropDowns设置为true,它可以正确设置下拉列表中的所有值,但它们被禁用而不是启用


怎么了

我认为您需要设置
enabled=“enabled”

尝试:


如果存在
disabled
属性(无论其值如何),则
select
元素将被禁用。因此,您需要这样的东西(将
htmlAttributes
指定为字典,而不是匿名对象,因为在本例中更方便):

@{
var displayMode=新字典();
添加(“onchange”,“LoadItems()”;
如果(ViewBag.EnableDropDowns)显示模式。添加(“禁用”、“禁用”);
}
@Html.DropDownList(“国家”,空,显示模式)

小心字典声明。它必须是
Dictionary()
,否则您将面临运行时问题

我必须根据条件禁用列表框

@{
var sourceListOptions = new Dictionary<string, object>();
sourceListOptions.Add("style", "Height: 250px; width: 225px;");
if (Model.SourceColumns.Count() == Model.ImportMappings.Count())
{
    sourceListOptions.Add("disabled", "disabled");
}


这使事情始终处于启用状态。。。我需要尝试下面的一个,看看今天晚些时候它是否解决了问题。这也不起作用。我可能没有提到我有2个dropdownlist对象,我需要这个动态变量来禁用它。我以前的方式很管用,但它是残疾的。我被告知尝试它的方法总是启用的。@Html.DropDownList(“Countries”,null,new{disabled=displayMode,onchange=“LoadItems()”})我可以知道您为什么将项的数据源作为null传递吗?现在这是一个挑战,我将解决它。我正在设置代码中的SelectList。。。“国家”的名称与ViewBag的名称相对应,这很复杂。
    @{
        bool displayMode = (ViewBag.EnableDropDowns) ? "enabled": "disabled";                     
     };

   @if(displayMode)
   {
     Html.DropDownList("Countries", null, 
      new { enabled= displayMode, onchange="LoadItems()" } );
   }
   else
   {
      Html.DropDownList("Countries", null, 
      new { disabled= displayMode, onchange="LoadItems()" } );
   }
@{ 
    var displayMode = new Dictionary<string,object>();
    displayMode.Add("onchange", "LoadItems()");
    if (ViewBag.EnableDropDowns) displayMode.Add("disabled", "disabled");
}

@Html.DropDownList("Countries", null, displayMode)
@{
var sourceListOptions = new Dictionary<string, object>();
sourceListOptions.Add("style", "Height: 250px; width: 225px;");
if (Model.SourceColumns.Count() == Model.ImportMappings.Count())
{
    sourceListOptions.Add("disabled", "disabled");
}
@Html.ListBox("SourceColumns", Model.SourceColumns, sourceListOptions)
@Html.DropDownList("SourceColumns", Model.SourceColumns, sourceListOptions)