
September 7th, 2008, 03:15 PM
| | | Create SelectList
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 | 
September 7th, 2008, 08:45 PM
| | | Re: Create SelectList
"shapper" <mdmoura@gmail.comwrote in message
news:b67ebe99-f95f-4cc5-8084-82fa6855d536@i76g2000hsf.googlegroups.com... Quote:
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 | 
September 7th, 2008, 10:25 PM
| | | Re: Create SelectList
On Sep 7, 8:36*pm, "Mark Rae [MVP]" <m...@markNOSPAMrae.netwrote: Quote:
"shapper" <mdmo...@gmail.comwrote in message
>
news:b67ebe99-f95f-4cc5-8084-82fa6855d536@i76g2000hsf.googlegroups.com...
> Quote:
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 | 
September 7th, 2008, 11:45 PM
| | | Re: Create SelectList
"shapper" <mdmoura@gmail.comwrote in message
news:23aff63d-0d2d-42b7-97b0-971296d6454f@y38g2000hsy.googlegroups.com... Quote: Quote: Quote:
>>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, stringMyDictionary = new Dictionary<string, string>();
MyDictionary.Add("New York", "NY");
--
Mark Rae
ASP.NET MVP http://www.markrae.net | 
September 8th, 2008, 12:15 PM
| | | Re: Create SelectList
On Sep 7, 11:36*pm, "Mark Rae [MVP]" <m...@markNOSPAMrae.netwrote: Quote:
"shapper" <mdmo...@gmail.comwrote in message
>
news:23aff63d-0d2d-42b7-97b0-971296d6454f@y38g2000hsy.googlegroups.com...
> Quote: Quote:
>How can I create the same list but with different name and value for
>each item?
| | > Quote: Quote:
I don't understand what you mean... How can it be the same list if it has
different contents...?
| | > Quote:
I just need to create a SelectList where each ListItem has different
Name and Value ...
| >> Quote: |
Item 1 Name = "New York", Value = "NY".
| >
Hmm - are you sure you're not talking about a Dictionary<>...?
>
Dictionary<string, stringMyDictionary = 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? | 
September 8th, 2008, 12:45 PM
| | | Re: Create SelectList
"shapper" <mdmoura@gmail.comwrote in message
news:f2d3782c-4e29-45a8-9d9e-05f47c7535f2@a70g2000hsh.googlegroups.com... Quote: Quote: Quote: |
>>Item 1 Name = "New York", Value = "NY".
| >>
>Hmm - are you sure you're not talking about a Dictionary<>...?
>>
>Dictionary<string, stringMyDictionary = 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 | 
September 8th, 2008, 01:05 PM
| | | Re: Create SelectList
On Sep 8, 12:34*pm, "Mark Rae [MVP]" <m...@markNOSPAMrae.netwrote: Quote:
"shapper" <mdmo...@gmail.comwrote in message
>
news:f2d3782c-4e29-45a8-9d9e-05f47c7535f2@a70g2000hsh.googlegroups.com...
> Quote: Quote: |
>Item 1 Name = "New York", Value = "NY".
| | > Quote: Quote: |
Hmm - are you sure you're not talking about a Dictionary<>...?
| | > Quote: Quote:
Dictionary<string, stringMyDictionary = new Dictionary<string,
string>();
MyDictionary.Add("New York", "NY");
| | > Quote: |
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 | 
September 8th, 2008, 01:55 PM
| | | Re: Create SelectList
"shapper" <mdmoura@gmail.comwrote in message
news:825b353a-2484-47c5-b1fd-e77cce615eda@r66g2000hsg.googlegroups.com... Quote: Quote: |
>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 | 
September 8th, 2008, 05:15 PM
| | | RE: Create SelectList
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: Quote:
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
>
| |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over network members.
|