-->

获得多个选定数值Html.DropDownlistFor(Getting Multiple Sele

2019-06-27 12:41发布

@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。 这些是存储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;
    }
}

除法的返回值被选择为真模型中的所选择的项目,但在观看侧它没有被选中。

Answer 1:

使用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财产。



Answer 2:

对我来说,它的工作原理也为@Html.DropDownListFor

模型:

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

控制器:

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" })

在控制器提交SelectedValues的样子:



文章来源: Getting Multiple Selected Values in Html.DropDownlistFor