[英]Getting Multiple Selected Values in Html.DropDownlistFor
@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,但在视图侧未选择。
使用ListBoxFor
而不是DropDownListFor
:
@Html.ListBoxFor(m => m.branch, CommonMethod.getBranch("", Model.branch), "--Select--")
@Html.ListBoxFor(m => m.division, CommonMethod.getDivision(Model.branch, Model.division), "--Select--")
branch
和division
属性显然必须是包含所选值的集合。
以及使用视图模型构建多选下拉列表的正确方法的完整示例:
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
属性匹配的项目。
对我来说,它也适用于@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 看起来像:
虽然很旧的线程,但在遵循其他答案之后发布了这个答案,不幸的是这对我不起作用。 所以,对于那些最近或不久的将来可能在这里偶然发现的人来说,下面是对我有用的。
我的问题是MultiSelectList
类,我使用的是SelectList
。
不知道 2012 年或 2015 年的情况。但是,现在这两个辅助方法@Html.DropDownListFor
和@Html.ListBoxFor
辅助方法都接受IEnumerable<SelectListItem>
因此您不能传递任何随机IEnumerable
对象并期望这些辅助方法执行工作。
这些辅助方法现在也接受SelectList
和MultiSelectList
类的对象,您可以在创建对象时直接传递所选值。
例如,请参阅下面的代码我如何创建我的多选下拉列表。
@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"
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.