当前位置: 代码迷 >> ASP.NET >> mvc3中如何限定@html.dropdownlist的值能否被选中
  详细解决方案

mvc3中如何限定@html.dropdownlist的值能否被选中

热度:2650   发布时间:2013-02-25 00:00:00.0
mvc3中怎么限定@html.dropdownlist的值能否被选中
mvc3中怎么限定@html.dropdownlist的值能否被选中

------解决方案--------------------------------------------------------
@{

var options = new List<SelectListItem>();

options.Add(new SelectListItem { Value = "1", Text = "1" });
options.Add(new SelectListItem { Value = "2", Text = "2" });
options.Add(new SelectListItem { Value = "3", Text = "3" });
options.Add(new SelectListItem { Value = "4", Text = "4" });
options.Add(new SelectListItem { Value = "5", Text = "5", Selected = true});

ViewData["options"] = options;
}
<select>
@{
foreach (var op in options)
{
if (op.Value == "2")
{
<option value='@op.Value' disabled="disabled"@(op.Selected ? " selected=selected" : "")>@op.Text</option>
}
else
{
<option value='@op.Value'@(op.Selected ? " selected='selected'" : "")>@op.Text</option>
}

}
}
</select>
  相关解决方案