我使用代码创建下拉菜单:
@Html.DropDownListFor(x => x.Material, Model.Materials, new { id = "ddlMaterial", onchange = "DisplayPageChangeMaterial();" })
但是我的选择需要看起来像:
<select name="Material" id="ddlMaterial" onchange="DisplayPageChangeMaterial();" multiple>
注意multiple
,它没有值。
我尝试过:
@Html.DropDownListFor(x => x.Material, Model.Materials, new { @multiple, id = "ddlMaterial", onchange = "DisplayPageChangeMaterial();" })
@Html.DropDownListFor(x => x.Material, Model.Materials, new { multiple, id = "ddlMaterial", onchange = "DisplayPageChangeMaterial();" })
@Html.DropDownListFor(x => x.Material, Model.Materials, new { "multiple", id = "ddlMaterial", onchange = "DisplayPageChangeMaterial();" })
全部失败。
您可以用两种方法编写。
1)具有DropDownListFor的对象htmlAttributes>
@Html.DropDownListFor(x => x.Material, Model.Materials, new { @data_custome = "my attributes values", @onchange = "DisplayPageChangeMaterial();", @id = "ddlMaterial", @multiple = "" })
输出
<select name="Material" data-custome="my attributes values" onchange="DisplayPageChangeMaterial();" id="ddlMaterial" multiple>
2)带DropDownListFor的IDictionary htmlAttributes>
@Html.DropDownListFor(x => x.Material, Model.Materials, new Dictionary<string, object>() { { "data-custome", "my attributes values" }, { "onchange", "DisplayPageChangeMaterial();" } , { "id", "ddlMaterial" }, { "multiple", "" } }
输出
<select name="Material" data-custome="my attributes values" onchange="DisplayPageChangeMaterial();" id="ddlMaterial" multiple>