在MVC4中隐藏DropDownList

时间:2013-08-07 06:07:18

标签: c# asp.net-mvc-4 hide html-select

我有一个简单的查询要求..

我有一个DropdownList,它包含的数据取决于DATABASE的值。

现在他们可以成为2个场景: -

1) - DropdownList保存值(非空) 2)-DropdownList不保存值(空)

现在我想要的是,我想隐藏DropdownList和LABEL(选择UserName),如果它是EMPTY ..我希望很清楚.. !!

我试过这个隐藏DropDownList但是它工作了,所以如何隐藏Label和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>
                }

我试过这个 - 当它变空时我禁用了我的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>
                }

5 个答案:

答案 0 :(得分:0)

试试这个

@if (Model.Items.Count() > 0)
{
    @Html.DropDownListFor(m => m.Selected, Model.Items);
    //Generate your dropdownlist here
}

答案 1 :(得分:0)

@if (ViewBag.UserName !=null && (ViewBag.UserName as IList<string>).Count >0)
{
    <label>
    Select UserName :</label>
    <div id="uniform-undefined" class="selector" 
                                 style="margin-right: 60px; margin-left: 10px;">
           @Html.DropDownList() // do something here
    </div>
}

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

答案 2 :(得分:0)

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

你的Viwe:

<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>

然后将此脚本添加到您的视图中:

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

答案 3 :(得分:0)

我通过简单地在标签中添加“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>
                }

答案 4 :(得分:0)

我尝试过以下操作并且工作正常:

@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" })
相关问题