MVC4中下拉菜单和单选框的简单设计方法
举例一:
@Html.LabelFor(model => model.Gender)
@Html.DropDownListFor(model => model.Gender, new[] { new SelectListItem() { Text = "女生", Value = "女生" }, new SelectListItem() { Text = "男生", Value = "男生" }, })
@* @Html.RadioButton("Gender", "男") 帅哥 @Html.RadioButton("Gender", "女") 美女 @Html.RadioButton("Gender", "保密", true) 保密*@
@Html.ValidationMessageFor(model => model.Gender)
举例二:
<tr>
<td style="text-align: right">他的最高身高:</td>
<td>
<select data-val="true" data-val-number="字段 年龄 必须是一个数字。" data-val-required="年龄 字段是必需的。" id="HightUl" name="HightUl">
<option value="0">请选择</option>
@for (int i = 163; i < 210; i++)
{
<option @(i == Model.HightUl ? "selected" : "") value="@i">@i</option>
}
</select>
</td>
</tr>
举例三:
<td>班级:</td>
<td>
@Html.DropDownList("CId", ViewBag.classList as IEnumerable<SelectListItem>)
@*<select name="CID">
@foreach (Class s in @ViewBag.classList as List<Class>)
{
if(s.CID == Model.CId){
<option selected value="@s.CID">@s.CName</option>
}else{
<option value="@s.CID">@s.CName</option>
}
}
</select>*@
</td>
</tr>
.Select( c=> new SelectListItem() { Text = c.CName, Value = c.CID.ToString(), Selected = (stu.CId == c.CID) }).ToList();//将实体集合 转成 SelectListItem集合
ViewBag.classList = listClass;