Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old September 7th, 2008, 03:15 PM
shapper
Guest
 
Posts: n/a
Default 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
  #2  
Old September 7th, 2008, 08:45 PM
Mark Rae [MVP]
Guest
 
Posts: n/a
Default 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

  #3  
Old September 7th, 2008, 10:25 PM
shapper
Guest
 
Posts: n/a
Default 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
  #4  
Old September 7th, 2008, 11:45 PM
Mark Rae [MVP]
Guest
 
Posts: n/a
Default 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

  #5  
Old September 8th, 2008, 12:15 PM
shapper
Guest
 
Posts: n/a
Default 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:
For example:
>
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?
  #6  
Old September 8th, 2008, 12:45 PM
Mark Rae [MVP]
Guest
 
Posts: n/a
Default 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

  #7  
Old September 8th, 2008, 01:05 PM
shapper
Guest
 
Posts: n/a
Default 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
  #8  
Old September 8th, 2008, 01:55 PM
Mark Rae [MVP]
Guest
 
Posts: n/a
Default 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

  #9  
Old September 8th, 2008, 05:15 PM
=?Utf-8?B?YnJ1Y2UgYmFya2Vy?=
Guest
 
Posts: n/a
Default 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
>
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

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.
Post your question now . . .
It's fast and it's free

Popular Articles