Create SelectList

S

shapper

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
 
S

shapper

I don't understand what you mean... How can it be the same list if it has
different contents...?

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
 
S

shapper

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

shapper

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

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
 
S

shapper

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

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
 
B

bruce barker

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,901
Members
47,439
Latest member
elif2sghost

Latest Threads

Top