下拉列表未在视图中显示

时间:2011-06-29 04:29:15

标签: asp.net-mvc asp.net-mvc-3 razor

我通过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 /> }
}

1 个答案:

答案 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元素。