.NET: DropDownList not showing up

本文关键字:showing up not DropDownList NET | 更新日期: 2023-09-27 18:26:36

当我运行此程序时,dropdownlist根本不会显示。它出现在更早的时候;我不确定我改变了什么使它不起作用。只显示提交按钮。

<div class="container">
    <h1>Enter Wellness Activity
    </h1>
    Select Activity Type:<br />
    @{
        List<Activity> acts = ViewBag.Activities;
        var selectItems = new SelectList(acts, "code", "shortTitle");
        ViewBag.selectItems = selectItems;
    }
    <div class="input">
        @{
            Html.BeginForm("ActivitySelected", "Home");
            Html.DropDownList("selectItems");
            <br />
           <button name="submit" value="Submit">Submit</button> 
           // Html.EndForm();
        }           
    </div>  
</div>

.NET: DropDownList not showing up

您需要对此代码进行三次更改才能使其正常工作。更改下拉列表,更改表单,并更改提交按钮,如下所示:

<div class="input">
    @using(Html.BeginForm("ActivitySelected", "Home"))
    {
        @Html.DropDownList("selectItems", ViewBag.selectItems)
        <br />
       <input type="submit" name="submit" value="Submit" />
    }
</div>

顺便说一句,不要在视图中将SelectList分配给ViewBag.selectItems,而是在控制器中执行。

一旦将项目设置到ViewBag.selectItems中,就不会对它们执行任何操作。

尝试将它们传递到下拉列表中:

Html.DropDownList("selectItems", ViewBag.selectItems);

尝试Html.DropDownList("selectItems",selectItems);

请参阅此过载方法:http://msdn.microsoft.com/en-us/library/gg548304(v=vs.111).aspx