我通过List<SelectListItem>
将ViewBag
传递给了视图。显示“组”的下拉列表,但不显示“组织”标签和下拉列表。
如果viewbag
不包含任何组/组,我想显示一个空的下拉控件。
可能会发生什么,不会出现第二个标签和下拉列表?
<div class="editor-label">
@Html.LabelFor(model => model.Group)
</div>
@{
var groupList = ViewBag.Group as List<Helpdesk.Models.GroupModel>;
if (groupList != null && groupList.Count > 0)
{
var groupItems = new SelectList(groupList, "ID", "Name");
<div class="dropdown-field">
@Html.DropDownListFor(model => model.Group.ID, @groupItems)
@Html.ValidationMessageFor(model => model.Group.ID)
</div>
}
else { <select /> }
}
<div class="editor-label">
@Html.LabelFor(model => model.Organization)
</div>
@{
var orgList = ViewBag.OrgList as List<Helpdesk.Models.OrganizationsModel>;
if (orgList != null && orgList.Count > 0)
{
var orgItems = new SelectList(orgList, "ID", "Name");
<div class="dropdown-field">
@Html.DropDownListFor(model => model.Organization.ID, @orgItems)
@Html.ValidationMessageFor(model => model.Organization.ID)
</div>
}
else { <select /> }
}
答案 0 :(得分:0)
你为什么不这样做:
if (orgList != null && orgList.Count > 0)
{
var orgItems = new SelectList(orgList, "ID", "Name");
<div class="dropdown-field">
@Html.DropDownListFor(model => model.Organization.ID, @orgItems)
@Html.ValidationMessageFor(model => model.Organization.ID)
</div>
}
else {
@Html.DropDownListFor(model => model.Organization.ID, new SelectListItem[] { })
}
推送一个空数组,而不是显示一个空的select元素。