ASP.NET MVC Dropdown List From SelectList

后端 未结 3 578
北海茫月
北海茫月 2020-11-28 06:02

I am building the following SelectList in my controller.

var u = new NewUser();

u.UserTypeOptions = new SelectList(new List

        
相关标签:
3条回答
  • 2020-11-28 06:30

    You are missing setting what field is the Text and Value in the SelectList itself. That is why it does a .ToString() on each object in the list. You could think that given it is a list of SelectListItem it should be smart enough to detect this... but it is not.

    u.UserTypeOptions = new SelectList(
        new List<SelectListItem>
        {
            new SelectListItem { Selected = true, Text = string.Empty, Value = "-1"},
            new SelectListItem { Selected = false, Text = "Homeowner", Value = ((int)UserType.Homeowner).ToString()},
            new SelectListItem { Selected = false, Text = "Contractor", Value = ((int)UserType.Contractor).ToString()},
        }, "Value" , "Text", 1);
    

    BTW, you can use a list of array of any type... and then just set the name of the properties that will act as Text and Value.

    I think it is better to do it like this:

    u.UserTypeOptions = new SelectList(
        new List<SelectListItem>
        {
            new SelectListItem { Text = "Homeowner", Value = ((int)UserType.Homeowner).ToString()},
            new SelectListItem { Text = "Contractor", Value = ((int)UserType.Contractor).ToString()},
        }, "Value" , "Text");
    

    I removed the -1 item, and the setting of each items selected true/false.

    Then, in your view:

    @Html.DropDownListFor(m => m.UserType, Model.UserTypeOptions, "Select one")
    

    This way, if you set the "Select one" item, and you don't set one item as selected in the SelectList, the UserType will be null (the UserType need to be int? ).

    If you need to set one of the SelectList items as selected, you can use:

    u.UserTypeOptions = new SelectList(options, "Value" , "Text", userIdToBeSelected);
    
    0 讨论(0)
  • 2020-11-28 06:34

    Try this, just an example:

    u.UserTypeOptions = new SelectList(new[]
        {
            new { ID="1", Name="name1" },
            new { ID="2", Name="name2" },
            new { ID="3", Name="name3" },
        }, "ID", "Name", 1);
    

    Or

    u.UserTypeOptions = new SelectList(new List<SelectListItem>
        {
            new SelectListItem { Selected = true, Text = string.Empty, Value = "-1"},
            new SelectListItem { Selected = false, Text = "Homeowner", Value = "2"},
            new SelectListItem { Selected = false, Text = "Contractor", Value = "3"},
        },"Value","Text");
    
    0 讨论(0)
  • 2020-11-28 06:38

    Just try this in razor

    @{
        var selectList = new SelectList(
            new List<SelectListItem>
            {
                new SelectListItem {Text = "Google", Value = "Google"},
                new SelectListItem {Text = "Other", Value = "Other"},
            }, "Value", "Text");
    }
    

    and then

    @Html.DropDownListFor(m => m.YourFieldName, selectList, "Default label", new { @class = "css-class" })
    

    or

    @Html.DropDownList("ddlDropDownList", selectList, "Default label", new { @class = "css-class" })
    
    0 讨论(0)
提交回复
热议问题