Create SelectList

This is a discussion on Create SelectList within the DOTNET forums in Framework and Interface Programming category; 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...

Go Back   Application Development Forum > Framework and Interface Programming > DOTNET

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 09-07-2008, 10:14 AM
shapper
Guest
 
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
Reply With Quote
  #2  
Old 09-07-2008, 03:36 PM
Mark Rae [MVP]
Guest
 
Default Re: Create SelectList

"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

Reply With Quote
  #3  
Old 09-07-2008, 05:24 PM
shapper
Guest
 
Default Re: Create SelectList

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
Reply With Quote
  #4  
Old 09-07-2008, 06:36 PM
Mark Rae [MVP]
Guest
 
Default Re: Create SelectList

"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

Reply With Quote
  #5  
Old 09-08-2008, 07:11 AM
shapper
Guest
 
Default Re: Create SelectList

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?
Reply With Quote
  #6  
Old 09-08-2008, 07:34 AM
Mark Rae [MVP]
Guest
 
Default Re: Create SelectList

"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

Reply With Quote
  #7  
Old 09-08-2008, 08:04 AM
shapper
Guest
 
Default Re: Create SelectList

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
Reply With Quote
  #8  
Old 09-08-2008, 08:47 AM
Mark Rae [MVP]
Guest
 
Default Re: Create SelectList

"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

Reply With Quote
  #9  
Old 09-08-2008, 12:11 PM
bruce barker
Guest
 
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:

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

Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 08:34 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.