Question

I have this list of string:

public static readonly List<string> LIST_ITEM_STATE = new List<string>
{
    "Brand new",
    "Never used",
    "Slightly used",
    "Used",
    "Heavily used",
    "Damaged",
    "Ruined",
    "Run for your life"
};

And I'm building a selectlist this way in a class:

public string mItemState { get; set; }
public IEnumerable<SelectListItem> mItemStateList { get; set; }

mItemStateList = new SelectList(ValueDomain.LIST_ITEM_STATE);

And, in my view, I render the dropdownlistfor like this:

Item State: @Html.DropDownListFor(_item => _item.mItemState, Model.mItemStateList, String.Empty)

The dropdownlist works like a charm, but I've been looking for ways to set the selected value by defaul at Brand New while keeping the String.Emtpty option. How could I do that?

Était-ce utile?

La solution

I think you'd have to do this:

public static readonly List<string> LIST_ITEM_STATE = new List<string>
{
    string.Empty,
    "Brand new",
    "Never used",
    "Slightly used",
    "Used",
    "Heavily used",
    "Damaged",
    "Ruined",
    "Run for your life"
};

And set the selected value in the SelectList:

 mItemStateList = new SelectList(ValueDomain.LIST_ITEM_STATE, "Brand new");

Or make sure the value of the the property is set to the initial value you want:

_item.mItemState = "Brand new";

Autres conseils

Define your SelectList like this:

mItemStateList = new SelectList(ValueDomain.LIST_ITEM_STATE, mItemState);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top