ListBoxFor不让我选择多个项目MVC [英] ListBoxFor not letting me select multiple items MVC
问题描述
当我运行代码时,我一次只能选择一个项目,这很奇怪,因为'ListBoxFor()'用于选择多个项目,所以我想要的是:
选择多个项目
查看(Index.cshtml):
< DIV>
@ Html.ListBoxFor(m => m.DropDownItems,new MultiSelectList(Repository.DDFetchItems(),Value,Text,Model.DropDownItems))
< / div>
模型(ModelVariables.cs):
public class ModelVariables
{
public List< SelectListItem> DropDownItems {get;组; }
}
public static class Repository
{
public static List< SelectListItem> DDFetchItems()
{
return new List< SelectListItem>()
{
New SelectListItem(){Text =Dogs,Value =1,Selected = true} ,
new SelectListItem(){Text =Cats,Value =2},
SelectListItem(){Text =Death,Value =3}
};
$ p (HomeController.cs):
pre $ $ $ $ $ $ $ $ $ $ $ $ $ {
$ {
$ $ b {
DropDownItems = Repository.DDFetchItems()
};
return View(model);
c $ c><多个> 到一组复杂对象(这就是 List< SelectListItem>
是)。 < select multiple>
回传一组简单值(在你的情况下,如果你选择第一个和第三个选项,它会提交您的模型需要一个
IEnumerable< int> (所选选项的值)。 / code>要绑定的属性。
public class ModelVariables
{
public IEnumerable< int> SelectedItems {get; set;}
public IEnumerable< SelectListItem> DropDownItems {get; set;}
}
然后在GET方法中
public ActionResult Index()
{
var ModelVariables = new ModelVariables()
{
DropDownItems = Repository.DDFetchItems(),
SelectedItems = new List< int>(){1,3} //预先选择第一和第三选项
};
返回视图(模型);
}
和在视图中
@ Html.ListBoxFor(m => m.SelectedItems,Model.DropDownItems)
附注
- 在
DDFetchItems()
方法中删除Selected = true
- 它的
被ListBoxFor()
方法忽略,因为它的
属性的值与绑定的值决定了选择的内容。
- 不需要从
第一个ListBoxFor()$ c $中创建一个新的相同的
SelectList
c>方法(propertyDropDownItems
已经是IEumerable< SelectListItem>
)
When I run the code, I can only select one item at a time, that's weird because 'ListBoxFor()' is used to select multiple items, so what i want is:
Select multiple items
View (Index.cshtml):
<div>
@Html.ListBoxFor(m => m.DropDownItems, new MultiSelectList(Repository.DDFetchItems(), "Value", "Text", Model.DropDownItems))
</div>
Model (ModelVariables.cs):
public class ModelVariables
{
public List<SelectListItem> DropDownItems { get; set; }
}
public static class Repository
{
public static List<SelectListItem> DDFetchItems()
{
return new List<SelectListItem>()
{
new SelectListItem(){ Text = "Dogs", Value = "1", Selected = true},
new SelectListItem(){ Text = "Cats", Value = "2"},
new SelectListItem(){ Text = "Death", Value = "3"}
};
}
}
Controller (HomeController.cs):
[HttpGet]
public ActionResult Index()
{
ModelVariables model = new ModelVariables()
{
DropDownItems = Repository.DDFetchItems()
};
return View(model);
}
You cannot bind a <select multiple>
to a collection of complex objects (which is what List<SelectListItem>
is). A <select multiple>
posts back an array of simple values (in your case, if you select the 1st and 3rd options, it will submit [1, 3]
(the values of the selected options).
Your model needs a IEnumerable<int>
property to bind to.
public class ModelVariables
{
public IEnumerable<int> SelectedItems { get; set; }
public IEnumerable<SelectListItem> DropDownItems { get; set; }
}
and then in the GET method
public ActionResult Index()
{
var ModelVariables= new ModelVariables()
{
DropDownItems = Repository.DDFetchItems(),
SelectedItems = new List<int>(){ 1, 3 } // to preselect the 1st and 3rd options
};
return View(model);
}
and in the view
@Html.ListBoxFor(m => m.SelectedItems, Model.DropDownItems)
Side notes
- Remove
Selected = true
in theDDFetchItems()
method - its ignored by theListBoxFor()
method because its the value of the property your binding to which determines what is selected - There is not need to build a new identical
SelectList
from the first one inside theListBoxFor()
method (propertyDropDownItems
is alreadyIEumerable<SelectListItem>
)
这篇关于ListBoxFor不让我选择多个项目MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!