Asp.net mvc 3 如何正确创建多选<;选择>;使用DropdownList帮助器?
(很抱歉,这里有几个项目,但似乎没有一个可以让我工作。) 我想创建一个下拉列表,允许多个选择。我可以填充列表,但无法使当前选定的值看起来正常工作 我的控制器中有以下各项:Asp.net mvc 3 如何正确创建多选<;选择>;使用DropdownList帮助器?,asp.net-mvc-3,drop-down-menu,selectlist,multi-select,html.listboxfor,Asp.net Mvc 3,Drop Down Menu,Selectlist,Multi Select,Html.listboxfor,(很抱歉,这里有几个项目,但似乎没有一个可以让我工作。) 我想创建一个下拉列表,允许多个选择。我可以填充列表,但无法使当前选定的值看起来正常工作 我的控制器中有以下各项: public ActionResult Index() { var model = new MyViewModel { // preselect the first and the third item given their ids SelectedIds = new[] {
public ActionResult Index()
{
var model = new MyViewModel
{
// preselect the first and the third item given their ids
SelectedIds = new[] { "1", "3" },
// fetch the items from some data source
Items = Enumerable.Range(1, 5).Select(x => new SelectListItem
{
Value = x.ToString(),
Text = "item " + x
})
};
return View(model);
}
ViewBag.PropertyGroups=来自db.eFinGroups中的g
其中g.GroupType.Contents==“P”
选择新的
{
键=g键,
值=g.说明,
所选=真
};
ViewBag.SelectedPropertyGroups=来自company.Entities中的g
.First().Properties.First().propertyGroup
选择新{
g、 efinggroup.Key,
值=g.eFinGroup.Description}代码>
我认为:
@Html.DropDownListFor(model=>model.PropertyGroupsX,
新的MultiSelectList(ViewBag.PropertyGroup
“键”、“值”
,ViewBag.SelectedPropertyGroup),
新建{@class=“chzn select”,数据占位符=“选择一个属性组”,多个=“多个”,style=“width:350px;”}
PropertyGroupX是模型中的字符串[]
我已经用选定的属性尝试了所有类型的迭代。。。只传递值、键、两者,等等
另外,PropertyGroupX应该是什么类型?字符串数组正确吗?还是应该是包含当前PropertyGroup的字典?我真的很难找到这方面的医生
有人建议我应该使用ListBoxFor。我已经改变了,仍然有同样的问题。呈现选项标记时,选定值不会设置为选定值。以下是我尝试过的:
@ListBoxFor(model=>model.PropertyGroups,新的MultiSelectList(ViewBag.PropertyGroups,“Key”,“Value”))
我尝试将model.PropertyGroups作为与值匹配的字符串集合,作为与此ID匹配的Guid集合,以及作为一个匿名类型,同时使用键和值来匹配ViewBag中的项。似乎什么都不起作用。如果要创建多选列表,则不需要使用DropDownListFor
。您可以为
帮助程序使用列表框
视图模型:
public class MyViewModel
{
public string[] SelectedIds { get; set; }
public IEnumerable<SelectListItem> Items { get; set; }
}
视图:
public class MyViewModel
{
public string[] SelectedIds { get; set; }
public IEnumerable<SelectListItem> Items { get; set; }
}
public ActionResult Index()
{
var model = new MyViewModel
{
// preselect the first and the third item given their ids
SelectedIds = new[] { "1", "3" },
// fetch the items from some data source
Items = Enumerable.Range(1, 5).Select(x => new SelectListItem
{
Value = x.ToString(),
Text = "item " + x
})
};
return View(model);
}
@model MyViewModel
@Html.ListBoxFor(x => x.SelectedIds, Model.Items)