tags:

views:

29

answers:

1

I have the following code:

// 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?

+1  A: 

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

chobo2
The 2nd solution works! So easy :) Thank you
Alex
Yep the second way is easier but if your using something like jquery validation(not sure about the current version) and you don't explicitly set the "text" and "value" and the jquery validation plugin gets activated and your using IE it will crash. That what I found anyways but they probably fixed it by now.
chobo2