Razor Html.DropDownListFor 自动设置选中值
Razor Html.DropDownListFor automatically set the selected value
我是 Razor 的新手,我将下拉列表作为 viewbag 和另一个值传递,当下拉列表在 web 中显示时它有选择的值但我没有设置任何选择的值,我想我使用相同的名称在 viewbag 值和 dropdownlist 值中,它会发生吗?
这是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>
}
页面模型似乎有LegislativeCode的值,
m => m.LegislativeCode
所以当ViewBag.Legislatives
中有相同的LegislativeCode
时,会选择这个选项。
我是 Razor 的新手,我将下拉列表作为 viewbag 和另一个值传递,当下拉列表在 web 中显示时它有选择的值但我没有设置任何选择的值,我想我使用相同的名称在 viewbag 值和 dropdownlist 值中,它会发生吗?
这是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>
}
页面模型似乎有LegislativeCode的值,
m => m.LegislativeCode
所以当ViewBag.Legislatives
中有相同的LegislativeCode
时,会选择这个选项。