SelectList选择的值不是从整数数组中分配的

时间:2015-11-16 12:33:24

标签: c# asp.net asp.net-mvc selectlist

如何设置SelectList

的selectedvalue属性
   var attributeSet = attribute.AttributeSetAttributes.Where(c => c.AttributeSetID == id).Select(c => c.AttributeID).ToArray();

   var attributeList = new SelectList(db.CatAttributes.Where(c => c.IsActive), "ID", "FullName", attributeSet);
    ViewBag.AttributesList = attributeList;

并且在视图中我像那样使用它

 @Html.ListBoxFor(model => model.Attributes, (SelectList)ViewBag.AttributesList, new { @class = "form-control" })

在上面的attributeSet获取integer个值中,SelectList => attributeList中所选属性应为true,如果我只给出一个值,则显示所选属性= true,但是当我传递一个数组或列表,它不会设置所选的属性= true

1 个答案:

答案 0 :(得分:0)

所以我修复它,我使用MultiSelectList而不是SelectList,并将第三个参数作为数组传递

var attributeSet = attribute.AttributeSetAttributes.Where(c => c.AttributeSetID == id).Select(c => c.AttributeID).ToArray();

var attributeList = new MultiSelectList(db.CatAttributes.Where(c => c.IsActive), "ID", "FullName");
ViewBag.AttributesList = attributeList;

var model = new AttributeSetViewModel()
        {
            AttributeSet = attribute, Attributes = attributeSet
        };

并在视图中

@Html.ListBoxFor(model => model.Attributes, (MultiSelectList)ViewBag.AttributesList, new { @class = "form-control" })

这会设置我选择的属性= true。

感谢@Stephen Muecke的想法!!