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

时间:2015-12-29 06:34:25

标签: c# html css razor

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

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

在我使用以下类型之前:

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

2 个答案:

答案 0 :(得分:1)

试试这个:

@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" },
       },
 new   {@class="select2_demo_4 form-control"})

答案 1 :(得分:1)

尝试使用此格式

@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)

在您的案例中htmlAttributesnew {@class="select2_demo_4 form-control"}

希望有所帮助!