开发者

How to set the selected index property for a SelectList with strings? (ASP.NET MVC)

开发者 https://www.devze.com 2022-12-25 04:08 出处:网络
I have the following code: //开发者_Go百科 Form the continuities list string[] continuities = new string[] { \"10s\", \"20s\", \"30s\" };

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

0

精彩评论

暂无评论...
验证码 换一张
取 消