Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在回发时设置多个SelectList值_C#_Asp.net Mvc_Asp.net Mvc 5_Bootstrap Selectpicker - Fatal编程技术网

C# 在回发时设置多个SelectList值

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",

我有一个包含多个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",
           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 edits
SystemTypes
是您的
选择列表
。您不能绑定到
选择列表
。您的属性需要是(比如)
IEnumerable SystemTypes
(一个
元素只返回一个简单值数组),并且不要使用
DropDownList()
-使用
ListBoxFor(m=>m.SystemTypes,RegistrationHandlers.GetSystemtypes(),new{…})
这是生成
@Stephen Muecke的正确方法最近你就像一个守护天使。感谢m8,您的
SelectList
属性名为
SystemTypes
,您显示的视图代码在任何地方都不使用该属性。您所显示的只是一个绑定到名为
UserRole
的属性的
DropDownList()
方法复制了错误的DropDownList,对不起,check edits
SystemTypes
是您的
选择列表
。您不能绑定到
选择列表
。您的属性需要是(比如)
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
属性:)