@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; }
}
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看起来像:
尽管线程很旧。对于那些可能在最近或不久后跌跌撞撞的人来说,以下是对我有用的。
所以,对我来说,要抓住的是,现在这些@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"
})