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

后端 未结 10 1805
情深已故
情深已故 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:35

    Try below code:

    @Html.DropDownList("ProductTypeID",null,"",new { @class = "form-control"})
    
    0 讨论(0)
  • 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<SelectListItem>(), new {@class="textbox"} )
    

    For a list with a few choices:

    @Html.DropDownList(
        "PriorityID", 
        new List<SelectListItem> 
        { 
            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

    0 讨论(0)
  • 2020-11-30 01:41

    If you are add more than argument ya dropdownlist in Asp.Net MVC. When you Edit record or pass value in view bag.

    Use this it will be work:-

    @Html.DropDownList("CurrencyID",null,String.Empty, new { @class = "form-control-mandatory" })
    
    0 讨论(0)
  • 2020-11-30 01: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}
    )
    
    0 讨论(0)
提交回复
热议问题