C# 向HTML.DropDownListFor添加类

C# 向HTML.DropDownListFor添加类,c#,asp.net-mvc,model-view-controller,C#,Asp.net Mvc,Model View Controller,我试图在下面的代码行中添加一个类赋值 @Html.DropDownListFor(model => model.Business, new SelectList( new List<Object>{ new { ... }, new { ... }, new { ... } },

我试图在下面的代码行中添加一个类赋值

        @Html.DropDownListFor(model => model.Business, new SelectList(
              new List<Object>{
                   new { ... },
                   new { ... },
                   new { ... }
                },
              "value",
              "text",
              2))
@Html.DropDownListFor(model=>model.Business,新选择列表(
新名单{
新{…},
新{…},
新{…}
},
“价值”,
“文本”,
2))
该站点()给出的一个答案建议将其添加为重载方法,该方法将“object HtmlatAttributes”作为最后一个参数。但是,我不能将此参数视为一个选项,因此我有点困惑这是如何实现的


在我的示例中,我使用了所有可能的重载方法,htmlattributes没有重载方法。

使用以下重载:

public static MvcHtmlString DropDownListFor<TModel, TProperty>(
this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TProperty>> expression,
IEnumerable<SelectListItem> selectList,
IDictionary<string, Object> htmlAttributes
)
public static MvcHtmlString DropDownListFor(
这个HtmlHelper HtmlHelper,
表情表情,
IEnumerable selectList,
辞典
)
这是

这样做:

@Html.DropDownListFor(model => model.Business, 
                      new SelectList(new List<Object>
                                 { 
                                  new { ... },
                                  new { ... },
                                  new { ... } 
                                }, 
                                "value", "text", 2),
                     new { @class ="YourClass"})
@Html.DropDownListFor(model=>model.Business,
新建选择列表(新建列表
{ 
新{…},
新{…},
新{…}
}, 
“价值”、“文本”2),
新建{@class=“YourClass”})
或以下过载也将起作用:

public static MvcHtmlString DropDownListFor<TModel, TProperty>(
this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TProperty>> expression,
IEnumerable<SelectListItem> selectList,
string optionLabel,
IDictionary<string, Object> htmlAttributes
)
public static MvcHtmlString DropDownListFor(
这个HtmlHelper HtmlHelper,
表情表情,
IEnumerable selectList,
字符串选项标签,
辞典
)
这样:

@Html.DropDownListFor(model => model.Business, 
                      new SelectList(new List<Object>
                                 { 
                                  new { ... },
                                  new { ... },
                                  new { ... } 
                                }, 
                                "value", "text", 2),
                     new { @class ="YourClass"})
@Html.DropDownListFor(model => model.Business, 
                      new SelectList(new List<Object> 
                                        { 
                                         new { ... }, 
                                         new { ... },
                                        }, "value", "text", 2), 
                      null,new { @class ="YourClass"})
@Html.DropDownListFor(model=>model.Business,
新建选择列表(新建列表
{ 
新{…},
新{…},
}“价值”、“文本”,2),
空,新的{@class=“YourClass”})

使用以下过载:

public static MvcHtmlString DropDownListFor<TModel, TProperty>(
this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TProperty>> expression,
IEnumerable<SelectListItem> selectList,
IDictionary<string, Object> htmlAttributes
)
public static MvcHtmlString DropDownListFor(
这个HtmlHelper HtmlHelper,
表情表情,
IEnumerable selectList,
辞典
)
这是

这样做:

@Html.DropDownListFor(model => model.Business, 
                      new SelectList(new List<Object>
                                 { 
                                  new { ... },
                                  new { ... },
                                  new { ... } 
                                }, 
                                "value", "text", 2),
                     new { @class ="YourClass"})
@Html.DropDownListFor(model=>model.Business,
新建选择列表(新建列表
{ 
新{…},
新{…},
新{…}
}, 
“价值”、“文本”2),
新建{@class=“YourClass”})
或以下过载也将起作用:

public static MvcHtmlString DropDownListFor<TModel, TProperty>(
this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TProperty>> expression,
IEnumerable<SelectListItem> selectList,
string optionLabel,
IDictionary<string, Object> htmlAttributes
)
public static MvcHtmlString DropDownListFor(
这个HtmlHelper HtmlHelper,
表情表情,
IEnumerable selectList,
字符串选项标签,
辞典
)
这样:

@Html.DropDownListFor(model => model.Business, 
                      new SelectList(new List<Object>
                                 { 
                                  new { ... },
                                  new { ... },
                                  new { ... } 
                                }, 
                                "value", "text", 2),
                     new { @class ="YourClass"})
@Html.DropDownListFor(model => model.Business, 
                      new SelectList(new List<Object> 
                                        { 
                                         new { ... }, 
                                         new { ... },
                                        }, "value", "text", 2), 
                      null,new { @class ="YourClass"})
@Html.DropDownListFor(model=>model.Business,
新建选择列表(新建列表
{ 
新{…},
新{…},
}“价值”、“文本”,2),
空,新的{@class=“YourClass”})

其中四个参数的最后一个参数是HTML属性。这可能是最简单的。修改示例后,它将变为:

@Html.DropDownListFor(model => model.Business, 
                      new SelectList(
                          new List<Object>{
                              new { ... },
                              new { ... },
                              new { ... }
                          },
                          "value",
                          "text",
                          2),
                      new {
                          @class = "myclass"
                      })
@Html.DropDownListFor(model=>model.Business,
新选择列表(
新名单{
新{…},
新{…},
新{…}
},
“价值”,
“文本”,
2),
新的{
@class=“myclass”
})
其中四个参数的最后一个参数是HTML属性。这可能是最简单的。修改示例后,它将变为:

@Html.DropDownListFor(model => model.Business, 
                      new SelectList(
                          new List<Object>{
                              new { ... },
                              new { ... },
                              new { ... }
                          },
                          "value",
                          "text",
                          2),
                      new {
                          @class = "myclass"
                      })
@Html.DropDownListFor(model=>model.Business,
新选择列表(
新名单{
新{…},
新{…},
新{…}
},
“价值”,
“文本”,
2),
新的{
@class=“myclass”
})

谢谢,我愚蠢地试图将它添加到实际的select语句部分中。这现在很好用。谢谢,我愚蠢地试图将它添加到实际的select语句部分中。现在这个效果很好。