How to set the selected index property for a SelectList with strings? (ASP.NET MVC)
I have the following code:
//开发者_Go百科 Form the continuities list
string[] continuities = new string[] { "10s", "20s", "30s" };
Model.Continuities = new SelectList(continuities, 2 );
I expect "20s" to be selected
How can I do that without creating a new class?
This is how I do it:
List<SelectListItem> list = new List<SelectListItem>();
SelectListItem select = new SelectListItem();
select.Text = "10"
select.Value = "10"
list.Add(select);
ViewData["Store"] = new SelectList(list, "text", "value", (object)"10");
I have not tested it but that's basically how it is in my code.
Edit
string[] continuities = new string[] { "10s", "20s", "30s" };
Model.Continuities = new SelectList(continuities, (object)"20s" );
I was also looking it takes to parameters so you might be able to do this
精彩评论