[英]Set multiple values as selected for "Html.DropDownListFor"
我手头有两个字符串数组
`mdm.Country` - Contains all the countries that needs to be displayed on the drop down.
`Model.Country` - Contains multiple selected items that needs to be marked as selected on the
drop down.
如何使用Html.DropDownListFor
来显示这种情况? 我尝试过这样的事情
@Html.DropDownListFor(n => n.Country, mdm.Country.Select
(d => { return new SelectListItem() { Selected = (d.ToString() == Model.Country), Text = d, Value = d }; }), null, new { @class = "custom", @multiple = "" })
但是给出了一个错误
运算符'=='不能应用于'string'和'string[]类型的操作数
谁能指出解决这个问题的正确方法。
由于Model.Country
是一个列表/数组,所以不应该使用==
而是使用.Contains()
来检查列表/数组中的值是否。
对于多选,建议使用Html.ListBoxFor()
@Html.ListBoxFor(n => n.Country,
mdm.Country
.Select(x => new SelectListItem
{
Selected = Model.Country.Contains(x),
Text = x,
Value = x
})
,
new { @class = "custom", @multiple = "" })
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.