palm snow
palm snow

Reputation: 2392

values for DropDownList items

I have follwoing code

@Html.DropDownList("optionsforuser", new SelectList(new[] { "Option1", "Option2", "Option3" }), "Select")

Is there anyway to initialize a value of 100 for Option1, 200 for Option2, 250 for Option3 etc within SelectList?

Upvotes: 1

Views: 5740

Answers (4)

Todd Menier
Todd Menier

Reputation: 39319

For situations where I just want to hard-code select list items (i.e. not get them from some pre-defined collection), I use this handy little class:

public class BetterSelectList : List<SelectListItem>
{
    public void Add(string text, object value, bool selected = false) {
        this.Add(new SelectListItem {
            Text = text, 
            Value = value.ToString(), 
            Selected = selected
        });
    }
}

Why is it "better"? It implements IEnumerable and has a multi-parameter Add method, which is all you need for the C# compiler to allow you to use dictionary-style initializers, resulting in about the most noise-free initialization possible:

var optionsForUser = new BetterSelectList {
    { "Option1", 100, true },
    { "Option2", 200 },
    { "Option3", 250 }
};

Upvotes: 1

Ron Sijm
Ron Sijm

Reputation: 8758

You can do it in your view like that as the others have posted, but can't you do it in the controller instead?

For example, In your view model:

public class ViewModel()
{
    public List<SelectListItem> OptionsForUser{ get; set; }
}

In your controller:

var optionsForUser = new List<SelectListItem>();
optionsForUser.Add(new SelectListItem { Text = "Option1", Value = "100"});
optionsForUser.Add(new SelectListItem { Text = "Option2", Value = "200"});
optionsForUser.Add(new SelectListItem { Text = "Option3", Value = "250"});

var viewmodel = new ViewModel();
viewmodel.OptionsForUser = optionsForUser;
return view(viewmodel);

then in your view:

@Html.DropDownList("optionsforuser", Model.OptionsForUser);

Those other solutions will work too, I just think its not very "clean" to initialise your dropdown list in your view like that. But its probably a matter of taste

Upvotes: 0

hunter
hunter

Reputation: 63522

Try using this Extension:

@Html.DropDownList("optionsforuser", 
    new SelectList(new Dictionary<string, int> 
        {
            {"Option1", 100},
            {"Option2", 200},
            {"Option3", 250}
        },
        "Value", "Key")
)

and pass it a Dictionary<string, int> with the dataValueField and dataTextField populated with your values and text


SelectExtensions.DropDownList Method (HtmlHelper, String, IEnumerable(Of SelectListItem))

public static MvcHtmlString DropDownList(
    this HtmlHelper htmlHelper,
    string name,
    IEnumerable<SelectListItem> selectList
)

SelectList Constructor (IEnumerable, String, String)

public SelectList(
    IEnumerable items,
    string dataValueField,
    string dataTextField
)

Upvotes: 5

Ryand.Johnson
Ryand.Johnson

Reputation: 1906

Yes you set text and value

var selections = new List<SelectListItem>();
selections.Add(new SelectListItem() { Text = "Option1", Value = "100" });
selections.Add(new SelectListItem() { Text = "Option2", Value ="200" });

Here is an MSDN article on select lists: select list

Upvotes: 0

Related Questions