如何在模型属性有值时设置 Html.DropDownListFor 的选定值并在属性为 null 时选择 optionLabel

发布于 2024-11-30 16:33:17 字数 306 浏览 2 评论 0原文

如果我的模型属性为空,我希望我的下拉列表自动选择 optionLabel。如果我的财产有价值的话,也会自动选择它的价值。

我遇到的情况是,无论我的模型属性有或没有值,我的下拉列表总是选择选项标签。

@Html.DropDownListFor(m => m.SportsId, new SelectList((IEnumerable<SelectListItem>)ViewData[ViewDataKeys.SportList], "Value", "Text"),"---",null)

谢谢!

I want my dropdown to automatically select the optionLabel if my model property is null. But also automatically select the value of my property if it has value.

What is happening to me is that my dropdownlist always select the optionlabel whether my model property has or has no value.

@Html.DropDownListFor(m => m.SportsId, new SelectList((IEnumerable<SelectListItem>)ViewData[ViewDataKeys.SportList], "Value", "Text"),"---",null)

thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文