I’m passing List<SelectListItem> to the view via ViewBag. The dropdown for “Groups” appears but the “Organizations” label and drop down doesn’t appear.
I want to show an empty dropdown control if the viewbag doesn’t contain any groups/org.
What might be happening that’s not making the 2nd label and dropdown appear?
<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 /> }
}
Why don’t you do this:
Push an empty array of items, instead of displaying an empty select element.