Asp.net mvc 4 返回MultiSelect DropDownList的选定对象

Asp.net mvc 4 返回MultiSelect DropDownList的选定对象,asp.net-mvc-4,Asp.net Mvc 4,我已将此DropDownList for添加到我的(部分)视图\u CreateUser.cshtml中,作为: <div class="modal fade bs-example-modal-lg" id="createUserModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div cl

我已将此DropDownList for添加到我的(部分)视图\u CreateUser.cshtml中,作为:

<div class="modal fade bs-example-modal-lg" id="createUserModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title">Create A User</h4>
    </div>
    @using (Html.BeginForm("Create", "Users", FormMethod.Post))
    {
        <div class="modal-body">

            @Html.LabelFor(model => model.Departments)
            <div>
                @Html.DropDownListFor(
                model => model.SelectedDepartmentIds,
                new SelectList(Model.DepartmentSelectList, "Value", "Text"),
                new {@class = "multiselect departments", multiple = "multiple"})
            </div>
        ...
UserViewModel.cs

public IEnumerable<String> Departments { get; set; }
//Used purely for binding DropDownList
public IEnumerable<SelectListItem> DepartmentSelectList { get; set; } 
public string SelectedDepartmentIds { get; set; }
//Ideally, I want to populate selected items in this list
public IEnumerable<DepartmentModel> DepartmentModels { get; set; } 
[HttpPost]
public ActionResult Create(UserViewModel postedModel)
{
        if (ModelState.IsValid)
        {
            //Incorrect - returns only first selected Id
            string selectedIds = postedModel.SelectedDepartmentIds;

            //Correct - returns correct selected Ids
            string selectedIds1 = ModelState["SelectedDepartmentId"].Value.AttemptedValue;
            ...
            ...
两个问题:

作为模型属性“SelectedDepartmentId”的一部分,如何从DropDownList中检索所有选定项?我是否需要在运行时使用jQuery更新ModalProperty

我是否可以设置视图,以便将模态属性“DepartmentModels”与DropDownList绑定?它将帮助我检索所选项目的完整对象? 目前,如果我尝试这样做,我会得到以下错误:
{“从类型'System.String'到类型'..DepartmentModel'的参数转换失败,因为没有类型转换器可以在这些类型之间转换。”}


谢谢大家!

在浏览了各种谷歌搜索后得到解决:-)

下面是正确加载和返回选定对象的代码-

型号:

public class UserGroupViewModel
{
    public IEnumerable<SelectListItem> GroupSelectList
    {
        get
        {
            List<Group> groups = new List<Group>
                {
                    new Group() {Id = 1, Name = "AA"},
                    new Group() {Id = 2, Name = "BB"},
                    new Group() {Id = 3, Name = "CC"}
                };
            IEnumerable<SelectListItem> groupsSelectList = groups.Select(group => new SelectListItem                                                                          {   Text = group.Name, Value = group.Id.ToString(), Selected = group.Id == 2}).ToList();
            return groupsSelectList;
        }
    }

    [Required]
    [Display(Name = "Group")]
    public int[] SelectedGroupIds { get; set; }
}
<div class="form-group">
    @Html.LabelFor(model => model.SelectedGroupIds, new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.DropDownListFor(model => model.SelectedGroupIds,
                                Model.GroupSelectList,
                                new { @class = "multiselect", multiple = "multiple" })
        @Html.ValidationMessageFor(model => model.SelectedGroupIds)
    </div>
</div>
public类UserGroupViewModel
{
公共IEnumerable组选择列表
{
收到
{
列表组=新列表
{
新组(){Id=1,Name=“AA”},
新组(){Id=2,Name=“BB”},
新组(){Id=3,Name=“CC”}
};
IEnumerable groups selectlist=groups.Select(group=>new SelectListItem{Text=group.Name,Value=group.Id.ToString(),Selected=group.Id==2}).ToList();
返回组选择列表;
}
}
[必需]
[显示(Name=“Group”)]
public int[]SelectedGroupIds{get;set;}
}
查看:

public class UserGroupViewModel
{
    public IEnumerable<SelectListItem> GroupSelectList
    {
        get
        {
            List<Group> groups = new List<Group>
                {
                    new Group() {Id = 1, Name = "AA"},
                    new Group() {Id = 2, Name = "BB"},
                    new Group() {Id = 3, Name = "CC"}
                };
            IEnumerable<SelectListItem> groupsSelectList = groups.Select(group => new SelectListItem                                                                          {   Text = group.Name, Value = group.Id.ToString(), Selected = group.Id == 2}).ToList();
            return groupsSelectList;
        }
    }

    [Required]
    [Display(Name = "Group")]
    public int[] SelectedGroupIds { get; set; }
}
<div class="form-group">
    @Html.LabelFor(model => model.SelectedGroupIds, new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.DropDownListFor(model => model.SelectedGroupIds,
                                Model.GroupSelectList,
                                new { @class = "multiselect", multiple = "multiple" })
        @Html.ValidationMessageFor(model => model.SelectedGroupIds)
    </div>
</div>

@LabelFor(model=>model.SelectedGroupIds,新的{@class=“controllabel col-md-2”})
@Html.DropDownListFor(model=>model.SelectedGroupIds,
Model.GroupSelectList,
新的{@class=“multiselect”,multiple=“multiple”})
@Html.ValidationMessageFor(model=>model.SelectedGroupId)
要在表单加载时将某些项显示为“选定”,只需将SelectListItem的“Selected”属性设置为true


HTH.

“如何从DropDownList中检索所有选定项目”。您使用的是
DropDownListFor
,它只允许选择一个项目。是否要选择多个项目?
控件只回发单个值,而不是复杂对象的所有属性。我使用jQuery允许多个选择,如:$('.multiselect').multiselect();在这种情况下,属性
selectedDepartmentId
可能应该是
string[]
(我不熟悉您使用的插件,但
通常返回一个值数组)