将自定义类添加到@ Html.DropDownListFor Asp.net Razor MVC

时间:2016-02-02 06:28:50

标签: asp.net razor asp.net-mvc-5

我想将自定义类添加到Asp.net Razor MVC控件@Html.DropDownListFor

我有以下代码

@Html.DropDownListFor(model => model.is_admin, new SelectList(
            new List<Object>{ 
                new { value = "true" , text = "Admin"  },  
                new { value = "false" , text = "User"  },                                               
             },                    
             "value",
             "text",
              "false" ))

如何在Asp.net MVC 5中实现相同的控件

1 个答案:

答案 0 :(得分:1)

添加第三个名为htmlAttributes的参数,如下所示。

@Html.DropDownListFor(model => model.is_admin,
       new SelectList(
           new List<Object>{
               new { value = "true" , text = "Admin"  },
               new { value = "false" , text = "User"  },

            }, "value", "text", "false"),
       new { @class = "your_class_name" });