如何将自定义属性分配给下拉列表

问题描述 投票:0回答:1

我使用代码创建下拉菜单:

@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();" })

全部失败。

asp.net-mvc attributes .net-4.5 html.dropdownlistfor
1个回答
0
投票

您可以用两种方法编写。

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>
© www.soinside.com 2019 - 2024. All rights reserved.