【问题标题】:Razor Html.DropDownListFor automatically set the selected valueRazor Html.DropDownListFor 自动设置选中值
【发布时间】: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


【解决方案1】:

看来页面模型对LegislativeCode有一个价值,

m => m.LegislativeCode

所以当ViewBag.Legislatives中有相同的LegislativeCode时,会选择这个选项。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-21
    相关资源
    最近更新 更多