Title: Insert value into selectlist
Tags: asp.net-mvc;selectlist
Question: I need to insert a value into a selectlist. It isn't the "0" value (i.e. the first one that shows) but the next one which would be "Other". This is then used to display an "Other" textbox.
My question is similiar to link text.
I don't know if this will help but here is the code:
SelectList Titles;
ViewData["TitleIsOther"] = TitleIsOther(Case);
if ((bool)ViewData["TitleIsOther"])
{
Titles = new SelectList((LookupCollection)this.LookupRepository.FetchByCategory(true, 0, 0, false,
(int)Enums.LookupCategory.CaseTitles, _LoggedInUser.AccountId), "Id", "Name", "-1");
}
else
{
Titles = new SelectList((LookupCollection)this.LookupRepository.FetchByCategory(true, 0, 0, false,
(int)Enums.LookupCategory.CaseTitles, _LoggedInUser.AccountId), "Id", "Name");
}
ViewData["Titles"] = Titles;
The selected value of "-1" would be the "Other" option.
Comment: If you post the controller code that creates the SelectList, someone may be able show you how to modify it to inject your extra item.


Here is another answer: You can't add 2nd value in similar fashion as optionLabel parameter of Html.DropDownList() provides. You will have to generate SelectList manually.
This post might be useful.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 2.5', 'CreationDate': '2009:00:08/12/09', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<asp.net-mvc><selectlist>', 'Title': 'Insert value into selectlist', 'ViewCount': 2099, 'id': 1265010, 'user_AboutMe': '<p>I just want to be me...</p>\n', 'user_AccountId': 11926.0, 'user_DisplayName': 'William Hurst', 'user_Reputation': 2191.0, 'user_Views': 181.0, 'user_id': 23230.0}}