Adding a css class to select using @Html.DropDownL

2020-01-27 01:59发布

I'm building my first MVC application after years of doing webforms, and for some reason I am not able to make this work:

@Html.DropDownList("PriorityID", String.Empty, new {@class="textbox"} )

Error message:

System.Web.Mvc.HtmlHelper<SPDR.Models.Bug>' does not contain a definition for DropDownList and the best extension method overload System.Web.Mvc.Html.SelectExtensions.DropDownList(System.Web.Mvc.HtmlHelper, string, System.Collections.Generic.IEnumerable<System.Web.Mvc.SelectListItem>, object) has some invalid arguments

Any help greatly appreciated!

10条回答
三岁会撩人
2楼-- · 2020-01-27 02:39

Try below code:

@Html.DropDownList("ProductTypeID",null,"",new { @class = "form-control"})
查看更多
三岁会撩人
3楼-- · 2020-01-27 02:44

There are some options in constructors look, if you don't have dropdownList and you wanna insert CSS class you can use like

@Html.DropDownList("Country", null, "Choose-Category", new {@class="form-control"})

in this case Country is the name of your dropdown, null is for you aren't passing any generic list from your controller "Choose-Category" is selected item and last one in CSS class if you don't wanna select any default option so simple replace "Choose-Category" with ""

查看更多
Emotional °昔
4楼-- · 2020-01-27 02:44

You Can do it using jQuery

  $("select").addClass("form-control")

here, Select is- html tag, Form-control is- class name

 @Html.DropDownList("SupplierId", "Select Supplier")

and here, SupplierId is ViewBagList, Select Supplier is - Display Name

查看更多
神经病院院长
5楼-- · 2020-01-27 02:44

Try this:

@Html.DropDownList(
    "country", 
    new[] {
        new SelectListItem() { Value = "IN", Text = "India" },
        new SelectListItem() { Value = "US", Text = "United States" }
    }, 
    "Country",
    new { @class = "form-control",@selected = Model.Country}
)
查看更多
做个烂人
6楼-- · 2020-01-27 02:47

Try This

 @Html.DropDownList("Id", null, new { @class = "ct-js-select ct-select-lg" })
查看更多
Juvenile、少年°
7楼-- · 2020-01-27 02:52

You can simply do this:

@Html.DropDownList("PriorityID", null, new { @class="form-control"})
查看更多
登录 后发表回答