在 PostBack 上设置多个 SelectList 值
Set multiple SelectList values on PostBack
我有一个包含多个 select 列表的表单,我还使用了 bootstrap select 选择器。
代码:
型号:
[Display(Name = "SystemTyp")]
[Required(ErrorMessage = "Vänligen välj typ")]
public List<SelectListItem> SystemTypes { get; set; }
查看:
<div class="form-group">
@Html.Label("SystemTyp", new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.DropDownList("SystemTypes",
RegistrationHandlers.GetSystemtypes()
,
new { @class = "form-control", @multiple = "multiple", @title = "--- Välj Systemtyp ---" })
@Html.ValidationMessageFor(model => model.SystemTypes, "", new { @class = "text-danger" })
</div>
</div>
当 posting:
每次我post列表都是空的。
列表名称与模型 属性 名称匹配。
我错过了什么?
我有另一个列表,它是一个 select,所以 selected 值是一个简单的字符串,它工作正常,但上面的内容让我很头疼。
您应该了解 DropDownList
帮助程序在 html 标记中创建具有 name="SystemTypes"
属性的 select
标签。
在 POST 中通过 selected 值 UserRole
名称。
而且您不需要 POST 上的完整列表,您只需要 selected 值,因此在 ViewModel
中创建 SystemTypeId
属性 并更改你的帮手:
@Html.DropDownList("SystemTypeId", <-- note this
RegistrationHandlers.GetSystemtypes()
,
new { @class = "form-control", @multiple = "multiple", @title = "--- Välj Systemtyp ---" })
然后您将在绑定模型中获得 selected 值。
不要试图取回 whoulde 列表 - 你不需要它。
如果你需要select多个你应该使用ListBox
助手:
@Html.ListBox("SystemTypeIds", <-- note this
RegistrationHandlers.GetSystemtypes()
,
new { @class = "form-control", @title = "--- Välj Systemtyp ---" })
SystemTypeIds
属性 应该是 Array
或 IEnumerable<int>
或 IList<int>
以绑定正确。 (当然,它不仅可以是 int
,还可以是 string
、bool
等。)
如果您正在寻找实现该目标的最佳方法,我建议您使用强类型助手 - ListBoxFor
:
@Html.ListBoxFor(x => x.SystemTypeIds
,RegistrationHandlers.GetSystemtypes()
,new { @class = "form-control", @title = "--- Välj Systemtyp ---" })
我有一个包含多个 select 列表的表单,我还使用了 bootstrap select 选择器。
代码:
型号:
[Display(Name = "SystemTyp")]
[Required(ErrorMessage = "Vänligen välj typ")]
public List<SelectListItem> SystemTypes { get; set; }
查看:
<div class="form-group">
@Html.Label("SystemTyp", new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.DropDownList("SystemTypes",
RegistrationHandlers.GetSystemtypes()
,
new { @class = "form-control", @multiple = "multiple", @title = "--- Välj Systemtyp ---" })
@Html.ValidationMessageFor(model => model.SystemTypes, "", new { @class = "text-danger" })
</div>
</div>
当 posting:
每次我post列表都是空的。 列表名称与模型 属性 名称匹配。
我错过了什么?
我有另一个列表,它是一个 select,所以 selected 值是一个简单的字符串,它工作正常,但上面的内容让我很头疼。
您应该了解 DropDownList
帮助程序在 html 标记中创建具有 name="SystemTypes"
属性的 select
标签。
在 POST 中通过 selected 值 UserRole
名称。
而且您不需要 POST 上的完整列表,您只需要 selected 值,因此在 ViewModel
中创建 SystemTypeId
属性 并更改你的帮手:
@Html.DropDownList("SystemTypeId", <-- note this
RegistrationHandlers.GetSystemtypes()
,
new { @class = "form-control", @multiple = "multiple", @title = "--- Välj Systemtyp ---" })
然后您将在绑定模型中获得 selected 值。
不要试图取回 whoulde 列表 - 你不需要它。
如果你需要select多个你应该使用ListBox
助手:
@Html.ListBox("SystemTypeIds", <-- note this
RegistrationHandlers.GetSystemtypes()
,
new { @class = "form-control", @title = "--- Välj Systemtyp ---" })
SystemTypeIds
属性 应该是 Array
或 IEnumerable<int>
或 IList<int>
以绑定正确。 (当然,它不仅可以是 int
,还可以是 string
、bool
等。)
如果您正在寻找实现该目标的最佳方法,我建议您使用强类型助手 - ListBoxFor
:
@Html.ListBoxFor(x => x.SystemTypeIds
,RegistrationHandlers.GetSystemtypes()
,new { @class = "form-control", @title = "--- Välj Systemtyp ---" })