【问题标题】:Viewbag New Selectlist PlaceholderViewbag 新选择列表占位符
【发布时间】:2018-08-01 02:47:06
【问题描述】:

我的操作方法中生成了一个简单的选择列表(代码如下)

Viewbag.Select = new SelectList(db.esp_admin_form_demographics.Where(x => x.category == (int) SelectHelper.ClientSelectOptions.Demographics.BirthCountry || x.category == 999), "id", "value", "");

如何在其中添加占位符值作为默认值?

【问题讨论】:

  • 占位符是什么意思?您是否希望将第一个选项设为(比如说)请选择
  • @StephenMuecke “占位符”是指设置为初始值的值,当SelectList 绑定到 DDL 或 DDLF 时将显示该值。类似问题herehere
  • @TetsuyaYamamoto,如果属性 OP 绑定到的唯一初始(默认)值与选项之一的值不匹配(这就是我问的原因)
  • @TAHASULTANTEMURI,我认为这不是 OP 所要求的,但无论如何,正如here 所解释的那样,答案是完全错误的(不知道它是如何被接受的)

标签: asp.net asp.net-mvc html.dropdownlistfor viewbag


【解决方案1】:

这是在 SelectList/DropDownList 中将占位符设置为默认值的解决方案:

在控制器方法中:

var espAdminList =  db.esp_admin_form_demographics.Where(x => x.category == (int) SelectHelper.ClientSelectOptions.Demographics.BirthCountry || x.category == 999)

Viewbag.EspAdminSelectList = new SelectList(espAdminList, "id", "value");

然后在视图中:

@Html.DropDownList("EspAdminId",ViewBag.EspAdminSelectList as SelectList, "Select Esp. Admin", new { @class = "form-control" })

【讨论】:

  • 你的代码不工作 - 你没有投ViewBag.EspAdminSelectList
  • 对不起!忘了!感谢您注意到这一点!我已经更新了答案
猜你喜欢
  • 2014-05-14
  • 1970-01-01
  • 2017-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-17
  • 2012-01-12
  • 1970-01-01
相关资源
最近更新 更多