Adding a css class to select using @Html.DropDownList()

后端 未结 10 1738
情深已故
情深已故 2020-11-30 01:13

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\"         


        
10条回答
  •  一向
    一向 (楼主)
    2020-11-30 01:37

    As the signature from the error message implies, the second argument must be an IEnumerable, more specifically, an IEnumerable of SelectListItem. It is the list of choices. You can use the SelectList type, which is a IEnumerable of SelectListItem. For a list with no choices:

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

    For a list with a few choices:

    @Html.DropDownList(
        "PriorityID", 
        new List 
        { 
            new SelectListItem { Text = "High", Value = 1 }, 
            new SelectListItem { Text = "Low",  Value = 0 },
        }, 
        new {@class="textbox"})
    

    Maybe this tutorial can be of help: How to create a DropDownList with ASP.NET MVC

提交回复
热议问题