C# 在MVC4中隐藏下拉列表

C# 在MVC4中隐藏下拉列表,c#,asp.net-mvc-4,hide,html-select,C#,Asp.net Mvc 4,Hide,Html Select,我有一个简单的问题要问 我有一个DropdownList,它保存的数据取决于数据库中的值 现在,它们可以是两种情况:- 1) -DropdownList保留值(非空) 2) -DropdownList不包含值(空) 现在我想要的是,我想隐藏下拉列表和标签(选择用户名),如果它是空的..我希望是清楚的 我试图隐藏DropDownList,但它无法工作,因此如何隐藏标签和DropDownList- <label> Select UserName :

我有一个简单的问题要问

我有一个DropdownList,它保存的数据取决于数据库中的值

现在,它们可以是两种情况:-

1) -DropdownList保留值(非空) 2) -DropdownList不包含值(空)

现在我想要的是,我想隐藏下拉列表和标签(选择用户名),如果它是空的..我希望是清楚的

我试图隐藏DropDownList,但它无法工作,因此如何隐藏标签和DropDownList-

<label>
                    Select UserName :</label>
                @if (@ViewBag.UserName.Items.Count == 0)
                {
                    <div id="uniform-undefined" class="selector" style="margin-right: 60px; margin-left: 10px;">
                        @Html.DropDownList("UserName", null, new { @visible= false })
                    </div>
                }

选择用户名:
@如果(@ViewBag.UserName.Items.Count==0)
{
@DropDownList(“用户名”,空,新{@visible=false})
}
我试过这个-我禁用了我的DropdownList,当它变空时,这个代码可以工作..如何

@if (@ViewBag.UserName.Items.Count == 0)
                {
                    <div id="uniform-undefined" class="selector" style="margin-right: 60px; margin-left: 10px;">
                        @Html.DropDownList("UserName", null, new { @disabled = true })
                    </div>
                }
@if(@ViewBag.UserName.Items.Count==0)
{
@DropDownList(“用户名”,空,新{@disabled=true})
}
试试这个

@if (Model.Items.Count() > 0)
{
    @Html.DropDownListFor(m => m.Selected, Model.Items);
    //Generate your dropdownlist here
}
@if(ViewBag.UserName!=null&&(ViewBag.UserName作为IList.Count>0)
{
选择用户名:
@Html.DropDownList()//在此处执行操作
}

试试这个,让我知道我没有试过这个

你可以添加一些jquery代码来实现你的目标:

您的意见:

<div id="divUsers">
    <label>Select UserName :</label>        
    <div id="uniform-undefined" class="selector" 
                                  style="margin-right: 60px; margin-left: 10px;">
        @Html.DropDownList("UserName")
    </div>        
</div>

选择用户名:
@Html.DropDownList(“用户名”)
然后将此脚本添加到视图中:

<script>
$(document).ready(function() {
    if (@ViewBag.UserName.Items.Count > 0)
        $("#divUsers").hide()
});
</script>

$(文档).ready(函数(){
如果(@ViewBag.UserName.Items.Count>0)
$(“#divUsers”).hide()
});

我通过简单地在标签中添加“display:none”解决了这个问题,如下所示:-

@if (@ViewBag.UserName.Items.Count == 0)
                {
                    <div id="uniform-undefined" class="selector" style="margin-right: 60px; margin-left: 10px; display: none ;">
                        @Html.DropDownList("UserName", null, new { @disabled = true })
                    </div>
                }
@if(@ViewBag.UserName.Items.Count==0)
{
@DropDownList(“用户名”,空,新{@disabled=true})
}

我尝试了以下方法,效果良好:

@Html.DropDownListFor(m => m.CLQty.Length, new SelectList(Model.Product.Length, length), new { @class = "form-control ",@disabled="disabled"})

@Html.DropDownListFor(m => m.CLQty.Length, new SelectList(Model.Product.Length, length), new { @class = "form-control ", @style="display:none" })

@ViewBag.UserName是用户名列表吗?只需将您的条件设置为大于零,并在其中生成您的DropDownstreer此链接我现在已编辑我的代码检查
@Html.DropDownListFor(m => m.CLQty.Length, new SelectList(Model.Product.Length, length), new { @class = "form-control ",@disabled="disabled"})

@Html.DropDownListFor(m => m.CLQty.Length, new SelectList(Model.Product.Length, length), new { @class = "form-control ", @style="display:none" })