【发布时间】:2021-08-05 18:50:25
【问题描述】:
我是 Razor 的新手,我将下拉列表作为 viewbag 和另一个值传递,当下拉列表在 web 中显示时它具有选定的值但我没有设置任何选定的值,我想我在 viewbag 中使用相同的名称value 和 dropdownlist value,会发生吗?
这是viewbag,我在这里设置值和下拉列表:
ViewBag.GlobalInstance = globalInstance;
ViewBag.LegislativeCode = legislativeCode.Trim();
ViewBag.Legislatives = new SelectList(legislativeEntities, "LegislativeCode", "LegislativeName");
这是前端代码:
@if (ViewBag.GlobalInstance == true)
{
<div class="col-xs-5 hcm-form_controller mandatory gap" >
<label for="LegislativeCode" class="w-100" mi-resourcekey="atr-LegislativeCode" >Legislative Entity</label>
@Html.DropDownListFor(m => m.LegislativeCode, ViewBag.Legislatives as SelectList, "--- Select ---", new { @class = "calc-w-100" })
</div>
}
【问题讨论】:
-
您能否发布页面上呈现的
select控件的实际html 代码?另请注意,默认情况下,选择列表中的第一个option项目将显示为已选中(如果没有其他选项项目具有selected属性集)
标签: c# visual-studio razor asp.net-core-mvc razor-pages