可以通过使用Html.DropDownListFor和Html.CheckBoxFor来创建一个包含复选框列表的下拉列表。以下是一个示例代码:
在ViewModel中添加数据模型:
public class MyViewModel
{
public string SelectedItem { get; set; }
public List
在Controller中添加数据:
public ActionResult Index()
{
var model = new MyViewModel();
model.Items = new List
model.CheckBoxItems = new List()
{
new SelectListItem() { Text = "CheckBox 1", Value = "1" },
new SelectListItem() { Text = "CheckBox 2", Value = "2" },
new SelectListItem() { Text = "CheckBox 3", Value = "3" },
new SelectListItem() { Text = "CheckBox 4", Value = "4" }
};
return View(model);
}
在View中添加HTML:
@model MyViewModel @using (Html.BeginForm()) { @Html.DropDownListFor(m => m.SelectedItem, Model.Items, new { @class = "form-control" })
@Html.CheckBoxFor(m => m.SelectedItems[0], Model.CheckBoxItems[0].Text, new { @class = "checkbox-inline" })
@Html.HiddenFor(m => m.CheckBoxItems[0].Value)
@Html.CheckBoxFor(m => m.SelectedItems[1], Model.CheckBoxItems[1].Text, new { @class = "checkbox-inline" })
@Html.HiddenFor(m => m.CheckBoxItems[1].Value)
@Html.CheckBoxFor(m => m.SelectedItems[2], Model.CheckBoxItems[2].Text, new { @class = "checkbox-inline" })
@Html.HiddenFor(m => m.CheckBoxItems[2].Value