2013-07-20 28 views
2

我是新来的MVC和工作的MVC4应用程序。Html.DropDownList禁用,如果它为空

我想要做的是我想禁用我的Dropdown,如果它是空的。

查看 - >

@Html.DropDownList("UserName", null, string.Empty) 

控制器 - >

ViewBag.UserName = new SelectList(lstUserName, "username", "username"); 

用户名是我的viewbag其中包含的项目列表来填充下拉列表。

现在,如果lstUserName是空的,我要禁用DropDown..How我可以做到这一点..

回答

1
@if (@ViewBag.UserName.Items.Count == 0) 
{ 
@Html.DropDownList("UserName", null, string.Empty, new { @disabled=true}) 
} 
else 
{ 
@Html.DropDownList("UserName", null, string.Empty) 
} 

你可以使用@readonly藏汉,而不是@disabled

+1

你的第一个版本不起作用。据我所知,'disabled'是基于属性的存在,而不是其中包含的值。 –

+0

非常感谢。它工作.. :) – Vishal

+0

第一版为我工作.. :) – Vishal

相关问题