如何将类添加到@ Html.DropDownList?

问题描述:

最初从模型加载下拉列值,现在需要加载静态值。所以我使用下面的类型来加载下拉列表,为此我无法添加CSS类。需要建议

Initially the dropdown values are loaded from the model, now it needs to be loaded static values. so I'm using the below type to load the dropdown, for this I can't able to add the CSS-class. Need advice

@Html.DropDownList("RoundOffInterval", new List<SelectListItem>
    {
    new SelectListItem{ Text="Select Roundoff Interval",Value=""},
    new SelectListItem{ Text="5",Value = "5" },
    new SelectListItem{ Text="10", Value = "10" },
    new SelectListItem{ Text="15", Value = "15" },
    new SelectListItem{ Text="20", Value = "20" },
    new SelectListItem{ Text="30", Value = "30" },})





之前我使用以下类型:



Before i used the below type :

@Html.DropDownListFor(m => m.RoundoffInterval, new SelectList(Model.RoundoffIntervalList, "Id", "Name"), "Select Roundoff Interval ", new { id = "RoundoffInterval", name = "RoundoffInterval", @class = "select2_demo_4 form-control" })

@Html.DropDownList("RoundOffInterval", new List<SelectListItem>
             {
             new SelectListItem{ Text="Select Roundoff Interval",Value=""},
             new SelectListItem{ Text="5",Value = "5" },
             new SelectListItem{ Text="10", Value = "10" },
             new SelectListItem{ Text="15", Value = "15" },
             new SelectListItem{ Text="20", Value = "20" },
             new SelectListItem{ Text="30", Value = "30" },"value","text",2}, new {@class="yourClassName"})


试试这种格式



Try with this format

@Html.DropDownList("RoundOffInterval", new List<SelectListItem>
{
  new SelectListItem{ Text="Select Roundoff Interval",Value=""},
  new SelectListItem{ Text="5",Value = "5" },
  new SelectListItem{ Text="10", Value = "10" },
  new SelectListItem{ Text="15", Value = "15" },
  new SelectListItem{ Text="20", Value = "20" },
  new SelectListItem{ Text="30", Value = "30" }
}, htmlAttributes)



在你的情况下使用 htmlAttributes new {@ class =select2_demo_4 form-control}



希望有所帮助!


With htmlAttributes in your case is new {@class="select2_demo_4 form-control"}

Hope it help!