c# - Razor Html.DropDownListFor 自动设置选中值
问题描述
我是 Razor 的新手,我将下拉列表作为 viewbag 和另一个值传递,当下拉列表在 web 中显示时它具有选定的值但我没有设置任何选定的值,我想我在 viewbag 值和下拉列表中使用相同的名称值,这可能发生吗?
这是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
因此,当 中存在相同时LegislativeCode
,ViewBag.Legislatives
将选择此选项。
推荐阅读
- ethereum - 智能合约可以检查当前区块中的交易吗?
- c# - Environment.CurrentDirectory 是如何工作的?
- flutter - 如何处理缓存,因为缓存包不适用于 pub.dev 中的颤振版本 2
- windows-subsystem-for-linux - 在 WSL 容器中的 CentOS 启动时运行挂载
- reactjs - React native:为什么我会收到“未捕获的错误:超出最大调用堆栈大小”?
- javascript - 无法读取 useEffect 中未定义的属性“样式”
- python - 在 3 组中打印可能的子字符串并与 python 中的字典键匹配
- intellij-idea - mac上的类似Intellij windows的绑定?
- c# - 如何在 C# 中将 JSON 数据转换为 Avro 模式?
- javascript - Auth0 在请求新的身份验证令牌时返回 401 未授权