| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
| |||
| |||
| Hello, I am creating a select list as follows: new SelectList(new[] { "a", "b"}) How can I create the same list but with different name and value for each item? Thanks, Miguel |
|
#2
| |||
| |||
| "shapper" <mdmoura@gmail.com> wrote in message news:b67ebe99-f95f-4cc5-8084-82fa6855d536@i76g2000hsf.googlegroups.com... > How can I create the same list but with different name and value for > each item? I don't understand what you mean... How can it be the same list if it has different contents...? -- Mark Rae ASP.NET MVP http://www.markrae.net |
|
#3
| |||
| |||
| On Sep 7, 8:36*pm, "Mark Rae [MVP]" <m...@markNOSPAMrae.net> wrote: > "shapper" <mdmo...@gmail.com> wrote in message > > news:b67ebe99-f95f-4cc5-8084-82fa6855d536@i76g2000hsf.googlegroups.com... > > > How can I create the same list but with different name and value for > > each item? > > I don't understand what you mean... How can it be the same list if it has > different contents...? > > -- > Mark Rae > ASP.NET MVPhttp://www.markrae.net No ... I just need to create a SelectList where each ListItem has different Name and Value ... For example: Item 1 > Name = "New York", Value = "NY". Thanks, Miguel |
|
#4
| |||
| |||
| "shapper" <mdmoura@gmail.com> wrote in message news:23aff63d-0d2d-42b7-97b0-971296d6454f@y38g2000hsy.googlegroups.com... >>> How can I create the same list but with different name and value for >>> each item? >> >> I don't understand what you mean... How can it be the same list if it has >> different contents...? > > I just need to create a SelectList where each ListItem has different > Name and Value ... > > For example: > > Item 1 > Name = "New York", Value = "NY". Hmm - are you sure you're not talking about a Dictionary<>...? Dictionary<string, string> MyDictionary = new Dictionary<string, string>(); MyDictionary.Add("New York", "NY"); -- Mark Rae ASP.NET MVP http://www.markrae.net |
|
#5
| |||
| |||
| On Sep 7, 11:36*pm, "Mark Rae [MVP]" <m...@markNOSPAMrae.net> wrote: > "shapper" <mdmo...@gmail.com> wrote in message > > news:23aff63d-0d2d-42b7-97b0-971296d6454f@y38g2000hsy.googlegroups.com... > > >>> How can I create the same list but with different name and value for > >>> each item? > > >> I don't understand what you mean... How can it be the same list if it has > >> different contents...? > > > I just need to create a SelectList where each ListItem has different > > Name and Value ... > > > For example: > > > Item 1 > Name = "New York", Value = "NY". > > Hmm - are you sure you're not talking about a Dictionary<>...? > > Dictionary<string, string> MyDictionary = new Dictionary<string, string>(); > MyDictionary.Add("New York", "NY"); > > -- > Mark Rae > ASP.NET MVPhttp://www.markrae.net I am using this in an DropDownList. Shouldn't be a SelectList? |
|
#6
| |||
| |||
| "shapper" <mdmoura@gmail.com> wrote in message news:f2d3782c-4e29-45a8-9d9e-05f47c7535f2@a70g2000hsh.googlegroups.com... >>> Item 1 > Name = "New York", Value = "NY". >> >> Hmm - are you sure you're not talking about a Dictionary<>...? >> >> Dictionary<string, string> MyDictionary = new Dictionary<string, >> string>(); >> MyDictionary.Add("New York", "NY"); > > I am using this in an DropDownList. Shouldn't be a SelectList? Perhaps we're getting bogged down by nomenclature here... Can you please clarify precisely which object you are referring to as a "SelectList"...? Some of your code would be helpful... -- Mark Rae ASP.NET MVP http://www.markrae.net |
|
#7
| |||
| |||
| On Sep 8, 12:34*pm, "Mark Rae [MVP]" <m...@markNOSPAMrae.net> wrote: > "shapper" <mdmo...@gmail.com> wrote in message > > news:f2d3782c-4e29-45a8-9d9e-05f47c7535f2@a70g2000hsh.googlegroups.com... > > >>> Item 1 > Name = "New York", Value = "NY". > > >> Hmm - are you sure you're not talking about a Dictionary<>...? > > >> Dictionary<string, string> MyDictionary = new Dictionary<string, > >> string>(); > >> MyDictionary.Add("New York", "NY"); > > > I am using this in an DropDownList. Shouldn't be a SelectList? > > Perhaps we're getting bogged down by nomenclature here... Can you please > clarify precisely which object you are referring to as a "SelectList"...? > > Some of your code would be helpful... > > -- > Mark Rae > ASP.NET MVPhttp://www.markrae.net Sure. I am creating in an ASP.NET MVC view a DropDownList: <%= Html.DropDownList( "", "Cities", new SelectList( new [] { "New York", "Paris", "London" } ) ) %> However, I would like each item to have a name different from its value. For example: "New York" would display as "New York" but the value would be "NY". Thanks, Miguel |
|
#8
| |||
| |||
| "shapper" <mdmoura@gmail.com> wrote in message news:825b353a-2484-47c5-b1fd-e77cce615eda@r66g2000hsg.googlegroups.com... >> Some of your code would be helpful... > > <%= Html.DropDownList( "", "Cities", new SelectList( new [] { "New > York", "Paris", "London" } ) ) %> > > However, I would like each item to have a name different from its > value. For example: > "New York" would display as "New York" but the value would be "NY". Ah right - I see your problem... You're using a single-dimension array to populate the DropDownList's Items collection - when you do this, the display value and the display text will be the same because that's all you're supplying. As I mentioned, you will need a Dictionary for this - however, I'm not sure if you'll be able to do this using the inline syntax above. Easy enough in code, though: http://bytes.com/forum/thread682026.html -- Mark Rae ASP.NET MVP http://www.markrae.net |
|
#9
| |||
| |||
| the SelectList supports any enumerable list. if you want an id and value in the constructor you specifiy a DataValueField and a DataTextField. as eval is done, you don't need to a use a data record, any object will do. try: new SelectList(new[] { new (ID = "1" Value = "a"}, new {ID = "2" Value ="b"} ), "ID","Value); -- bruce (sqlwork.com) "shapper" wrote: > Hello, > > I am creating a select list as follows: > > new SelectList(new[] { "a", "b"}) > > How can I create the same list but with different name and value for > each item? > > Thanks, > Miguel > |
![]() |
| Thread Tools | |
| Display Modes | |
In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.