C# 在 Html.DropDownlistFor 中获取多个选定值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12176735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting Multiple Selected Values in Html.DropDownlistFor
提问by user
@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" })
I have two instances of DropDownListFor. I want to set selected as true for those which have previously stored values for Model.branch and Model.division. These are string arrays of stored ids
我有两个 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;
}
}
The returned value of division is selected as true for the selected item in the model, but on view side it is not selected.
对于模型中的选定项,除法的返回值选择为 true,但在视图侧未选择。
采纳答案by Darin Dimitrov
Use a ListBoxForinstead of DropDownListFor:
使用ListBoxFor代替DropDownListFor:
@Html.ListBoxFor(m => m.branch, CommonMethod.getBranch("", Model.branch), "--Select--")
@Html.ListBoxFor(m => m.division, CommonMethod.getDivision(Model.branch, Model.division), "--Select--")
The branchand divisionproperties must obviously be collections that will contain the selected values.
该branch和division性能显然必须将包含所选值的集合。
And a full example of the proper way to build a multiple select dropdown using a view model:
以及使用视图模型构建多选下拉列表的正确方法的完整示例:
public class MyViewModel
{
public int[] SelectedValues { get; set; }
public IEnumerable<SelectListItem> Values { get; set; }
}
that would be populated in the controller:
这将填充在控制器中:
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);
}
and in the view:
并在视图中:
@model MyViewModel
...
@Html.ListBoxFor(x => x.SelectedValues, Model.Values)
It is the HTML helper that will automatically preselect the items whose values match those of the SelectedValuesproperty.
HTML 帮助程序将自动预选其值与SelectedValues属性匹配的项目。
回答by tonco
For me it works also for @Html.DropDownListFor:
对我来说,它也适用于@Html.DropDownListFor:
Model:
模型:
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);
}
Razor:
剃刀:
@Html.DropDownListFor(m => m.SelectedValues, Model.Values, new { multiple = "true" })
Submitted SelectedValues in controller looks like:
控制器中提交的 SelectedValues 看起来像:
回答by Div Tiwari
Though quite old thread but posting this answer after following other answers here, which unfortunately didn't work for me. So, for those who might have stumbled here recently or in near future, Below is what has worked for me.
虽然很旧的线程,但在遵循其他答案之后发布了这个答案,不幸的是这对我不起作用。所以,对于那些最近或不久的将来可能在这里偶然发现的人来说,下面是对我有用的。
The catch for me was MultiSelectListclass and I was using SelectList.
对我来说最重要的是MultiSelectList课堂,我正在使用SelectList.
Don't know situation in 2012 or 2015. but, now both these helper methods @Html.DropDownListForand @Html.ListBoxForhelper methods accept IEnumerable<SelectListItem>so you can not pass any random IEnumerableobject and expect these helper methods to do the job.
不知道 2012 年或 2015 年的情况。但是,现在这些辅助方法@Html.DropDownListFor和@Html.ListBoxFor辅助方法都接受,IEnumerable<SelectListItem>因此您不能传递任何随机IEnumerable对象并期望这些辅助方法完成这项工作。
These helper methods now also accept the object of SelectListand MultiSelectListclasses in which you can pass the selected values directly while creating there objects.
这些辅助方法现在也接受对象SelectList和MultiSelectList类,您可以在创建对象时直接在其中传递所选值。
For example see below code how i created my multi select drop down list.
例如,请参阅下面的代码我如何创建我的多选下拉列表。
@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"
})


