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>