C# 在回发时设置多个SelectList值
我有一个包含多个selectlist的表单,我还使用bootstrap selectpicker 代码:C# 在回发时设置多个SelectList值,c#,asp.net-mvc,asp.net-mvc-5,bootstrap-selectpicker,C#,Asp.net Mvc,Asp.net Mvc 5,Bootstrap Selectpicker,我有一个包含多个selectlist的表单,我还使用bootstrap selectpicker 代码: <div class="form-group"> @Html.Label("SystemTyp", new { @class = "col-md-2 control-label" }) <div class="col-md-10"> @Html.DropDownList("SystemTypes",
<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>
型号:
[Display(Name = "SystemTyp")]
[Required(ErrorMessage = "Vänligen välj typ")]
public List<SelectListItem> SystemTypes { get; set; }
[显示(Name=“SystemTyp”)]
[必需(ErrorMessage=“Vänligen Välj typ”)]
公共列表系统类型{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>
@Label(“SystemTyp”,新的{@class=“col-md-2控制标签”})
@Html.DropDownList(“系统类型”,
RegistrationHandlers.GetSystemtypes()
,
新的{@class=“form control”、@multiple=“multiple”、@title=“--Välj Systemtyp-->”)
@Html.ValidationMessageFor(model=>model.SystemTypes,“,new{@class=“text danger”})
张贴时:
每次我发布时,列表都是空的。
列表名称与模型属性名称匹配
我错过了什么
我还有一个单选列表,所以选择的值是一个简单的字符串,这很好,但上面的内容让我头疼 您应该理解
DropDownList
helper在html标记中创建select
带有name=“SystemTypes”
属性的标记
OnPOSTin将所选值与UserRole
name一起传递
您不需要POST上的完整列表,您只需要选择的值,因此在ViewModel
中创建SystemTypeId
属性,并将助手更改为:
@Html.DropDownList("SystemTypeId", <-- note this
RegistrationHandlers.GetSystemtypes()
,
new { @class = "form-control", @multiple = "multiple", @title = "--- Välj Systemtyp ---" })
SystemTypeIds
属性应为Array
或IEnumerable
或IList
以绑定更正。(当然,它不仅可以是int
,还可以是string
,bool
等。)
如果您正在寻找实现这一目标的最佳方法,我建议您使用强类型帮助程序-ListBoxFor
:
@Html.ListBoxFor(x => x.SystemTypeIds
,RegistrationHandlers.GetSystemtypes()
,new { @class = "form-control", @title = "--- Välj Systemtyp ---" })
您应该知道
DropDownList
helper在html标记中创建select
tag和name=“SystemTypes”
属性
OnPOSTin将所选值与UserRole
name一起传递
您不需要POST上的完整列表,您只需要选择的值,因此在ViewModel
中创建SystemTypeId
属性,并将助手更改为:
@Html.DropDownList("SystemTypeId", <-- note this
RegistrationHandlers.GetSystemtypes()
,
new { @class = "form-control", @multiple = "multiple", @title = "--- Välj Systemtyp ---" })
SystemTypeIds
属性应为Array
或IEnumerable
或IList
以绑定更正。(当然,它不仅可以是int
,还可以是string
,bool
等。)
如果您正在寻找实现这一目标的最佳方法,我建议您使用强类型帮助程序-ListBoxFor
:
@Html.ListBoxFor(x => x.SystemTypeIds
,RegistrationHandlers.GetSystemtypes()
,new { @class = "form-control", @title = "--- Välj Systemtyp ---" })
您的
SelectList
属性名为SystemTypes
,您显示的视图代码在任何地方都不使用该属性。您所显示的只是一个绑定到名为UserRole
的属性的DropDownList()
方法复制了错误的DropDownList,对不起,check editsSystemTypes
是您的选择列表
。您不能绑定到选择列表
。您的属性需要是(比如)IEnumerable SystemTypes
(一个
元素只返回一个简单值数组),并且不要使用DropDownList()
-使用ListBoxFor(m=>m.SystemTypes,RegistrationHandlers.GetSystemtypes(),new{…})
这是生成
@Stephen Muecke的正确方法最近你就像一个守护天使。感谢m8,您的SelectList
属性名为SystemTypes
,您显示的视图代码在任何地方都不使用该属性。您所显示的只是一个绑定到名为UserRole
的属性的DropDownList()
方法复制了错误的DropDownList,对不起,check editsSystemTypes
是您的选择列表
。您不能绑定到选择列表
。您的属性需要是(比如)IEnumerable SystemTypes
(一个
元素只返回一个简单值数组),并且不要使用DropDownList()
-使用ListBoxFor(m=>m.SystemTypes,RegistrationHandlers.GetSystemtypes(),new{…})
这是生成
@Stephen Muecke的正确方法最近你就像一个守护天使。谢谢M8对不起,在我的困惑和沮丧中,我复制了错误的代码。检查编辑。我只想要所选的值您描述的是如果我只在列表中选择一项。但是当我选择multiple时,我建议使用ListBoxFor()
方法,并将Model属性传递给它,这样可以避免重构时的麻烦:)@teovankot也建议这样做。是的,我们来这里是为了帮助尝试和解决OP的问题-但是如果我们能建议更好的实践来解决更深层次的问题,那么所有的奖励都会得到ListBox()
和ListBoxFor()
添加multlple
属性:)对不起,在我的困惑和沮丧中,我复制了错误的代码。检查编辑。我只想要所选的值您描述的是如果我只在列表中选择一项。但是当我选择multiple时,我建议使用ListBoxFor()
方法,并将Model属性传递给它,这样可以避免重构时的麻烦:)@teovankot也建议这样做。是的,我们来这里是为了帮助尝试和解决OP的问题-但是如果我们能建议更好的实践来解决更深层次的问题,那么所有的奖励都会得到ListBox()
和ListBoxFor()
添加multlple
属性:)