在Html.DropDownlistFor中获取多个选定值

问题描述 投票:33回答:3
@Html.DropDownListFor(m => m.branch, CommonMethod.getBranch("",Model.branch), "--Select--", new { @multiple = "multiple" })

@Html.DropDownListFor(m => m.division, CommonMethod.getDivision(Model.branch,Model.division), "--Select--", new { @multiple = "multiple" })

我有两个DropDownListFor实例。我想将那些以前存储了Model.branch和Model.division值的值设置为true。这些是存储的ID的字符串数组

class CommonMethod
{
    public static List<SelectListItem> getDivision(string [] branchid , string [] selected)
    {
        DBEntities db = new DBEntities();
        List<SelectListItem> division = new List<SelectListItem>();
        foreach (var b in branchid)
            {
                var bid = Convert.ToByte(b);
                var div = (from d in db.Divisions where d.BranchID == bid select d).ToList();
                foreach (var d in div)
                {
                    division.Add(new SelectListItem { Selected = selected.Contains(d.DivisionID.ToString()), Text = d.Description, Value = d.DivisionID.ToString() });
                }
            }
        }

        return division;
    }
}

对于模型中所选项目,将除法的返回值选择为true,但在视图侧未选择。

c# asp.net-mvc asp.net-mvc-3 razor html.dropdownlistfor
3个回答
60
投票

使用ListBoxFor代替DropDownListFor

@Html.ListBoxFor(m => m.branch, CommonMethod.getBranch("", Model.branch), "--Select--")

@Html.ListBoxFor(m => m.division, CommonMethod.getDivision(Model.branch, Model.division), "--Select--")

branchdivision属性显然必须是将包含所选值的集合。

以及使用视图模型构建多选下拉列表的正确方法的完整示例:

public class MyViewModel
{
    public int[] SelectedValues { get; set; }
    public IEnumerable<SelectListItem> Values { get; set; }
}

将在控制器中填充:

public ActionResult Index()
{
    var model = new MyViewModel();

    // preselect items with values 2 and 4
    model.SelectedValues = new[] { 2, 4 };

    // the list of available values
    model.Values = new[]
    {
        new SelectListItem { Value = "1", Text = "item 1" },
        new SelectListItem { Value = "2", Text = "item 2" },
        new SelectListItem { Value = "3", Text = "item 3" },
        new SelectListItem { Value = "4", Text = "item 4" },
    };

    return View(model);
}

并且在视图中:

@model MyViewModel
...
@Html.ListBoxFor(x => x.SelectedValues, Model.Values)

这是HTML帮助程序,它将自动预选择其值与SelectedValues属性的值匹配的项目。


11
投票

对我来说,它也适用于@Html.DropDownListFor

型号:

public class MyViewModel
{
    public int[] SelectedValues { get; set; }
    public IEnumerable<SelectListItem> Values { get; set; }
}

Controller:

public ActionResult Index()
{
    var model = new MyViewModel();

    // the list of available values
    model.Values = new[]
    {
        new SelectListItem { Value = "2", Text = "2", Selected = true },
        new SelectListItem { Value = "3", Text = "3", Selected = true },
        new SelectListItem { Value = "6", Text = "6", Selected = true }
    };

    return View(model);
}

剃刀:

@Html.DropDownListFor(m => m.SelectedValues, Model.Values, new { multiple = "true" })

控制器中的Submitted SelectedValues看起来像:

enter image description hereenter image description here


0
投票

尽管线程很旧。对于那些可能在最近或不久后跌跌撞撞的人来说,以下是对我有用的。

This is what helped me

所以,对我来说,要抓住的是,现在这些@Html.DropDownListFor@Html.ListBoxFor帮助器方法都接受IEnumerable<SelectListItem>,因此您不能传递任何随机的IEnumerable对象并期望这些帮助器方法可以完成工作。

这些帮助器方法现在还接受SelectListMultiSelectList类的对象,您可以在其中创建对象时直接传递所选值。

例如,请参见下面的代码,我如何创建多选下拉列表。

@Html.DropDownListFor(model => @Model.arrSelectUsers, new MultiSelectList(Model.ListofUsersDTO, "Value", "Text", @Model.arrSelectUsers),
                                                    new
                                                    {
                                                        id = "_ddlUserList",
                                                        @class = "form-control multiselect-dropdown",
                                                        multiple = "true",
                                                        data_placeholder = "Select Users"
                                                    })
© www.soinside.com 2019 - 2024. All rights reserved.